<?php
namespace Products\Controller;
use Hybrid_Auth;
use ScnSocialAuth\Mapper\Exception as MapperException;
use ScnSocialAuth\Mapper\UserProviderInterface;
use ScnSocialAuth\Options\ModuleOptions;
use Zend\View\Model\ModelInterface;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Users\Model\User;
use Users\Model\AdminCombinations;
use Users\Model\UserRegions;
use Products\Model\Categories;
use Products\Model\Products;
use Products\Model\Productsnew;
use Products\Model\Offers;
use Products\Model\Buy;
use Products\Model\History;
use Products\Model\Bid;
use Products\Model\CountryAndIndustry;
use Users\Form\UserForm; 
use Zend\View\Model\JsonModel;
use Zend\Session\Container;
use Users\Model\Forgetpassword;
use Application\Model\country;
use Application\Model\state;
use Products\Model\Industry;
use products\Model\IndustryTable;
use Users\Model\Usersnew;
use products\Model\UsersnewTable;

class ProductsController extends AbstractActionController
{
	protected  $userTable;
	protected  $offersTable;
	protected  $forgetpasswordTable;	
	protected  $countryTable;
	protected  $stateTable;
	protected  $categoriesTable;
	protected  $productsTable;
	protected  $buyTable;
	protected  $bidTable;
	protected  $productsnewTable;
	protected  $IndustryTable;
	protected  $usersnewTable;
	protected  $adminCombinationsTable;
	protected  $userRegionsTable;
	protected  $historyTable;
	protected  $countryAndIndustryTable;
	  /**
     * @var UserProviderInterface
     */
    protected $mapper;

    /**
     * @var Hybrid_Auth
     */
    protected $hybridAuth;

    /**
     * @var ModuleOptions
     */
    protected $options;

    /*
     * @todo Make this dynamic / translation-friendly
     * @var string
     */  
    public function indexAction()
	{
	}
	public function updateProductStatusAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');	
		global $changeStatusBuyerSubject;
		global $changeStatusBuyerMessage;
		global $buyerparentSubject;
		global $buyerparentMessage;		
		global $changestatusproductownerSubject;
		global $changestatusproductownerMessage;		
		global $productownerparentSubject;
		global $productownerparentMessage;
		global $superadminSubject;
		global $superadminMessage;
		if(isset($_POST['id']) && $_POST['id']!=""){
			$user_product_id=$_POST['hid_product_id'];
			$userTable = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$buyDetails = $userTable->editUser($_POST['buyer_id']);	
			if(isset($_POST['newStatus']) && $_POST['newStatus']!=""){
				if($_POST['newStatus']==0){
					$ststus='Pending';
				}else if($_POST['newStatus']==1){
					$ststus='Processing';
				}else if($_POST['newStatus']==2){
					$ststus='Processed';
				}else if($_POST['newStatus']==3){
					$ststus='Canceld';
				}	
			}
			$message=$_POST['note'];
			//Buyer Deatils
			$buyerEmail=$buyDetails->email;
			$buyerName=$buyDetails->company_name;
			$buyerParentId=$buyDetails->parent_user_id;
			$productOwnerDetails = $userTable->editUser($_POST['buyer_id']);
			//Product Owner Deatils
			$productownerEmail=$productOwnerDetails->email;
			$productOwnerName=$productOwnerDetails->company_name;
			$productownerParent=$productOwnerDetails->parent_user_id;
			if($buyerParentId!=$productownerParent){
				//product owner parent details 
				$productOwnerparentDetails = $userTable->editUser($productownerParent);
				$productOwnerparentDetailsEmail=$productOwnerparentDetails->email;
				$productOwnerparentDetailsName=$productOwnerparentDetails->company_name;
				$productOwnerparentDetailsParent=$productOwnerparentDetails->parent_user_id;
				//Buyer parent
				$buyerparent = $userTable->editUser($buyerParentId);
				$buyerparentEmail=$buyerparent->email;
				$buyerparentName=$buyerparent->company_name;
				$buyerparentParent=$buyerparent->parent_user_id;
				//super admin
				$superadmin = $userTable->editUser($buyerParentId);
				$superadmintEmail=$superadmin->email;
				$superadminName=$superadmin->company_name;				
			}else{
				//Buyer and product owner details
				$buyerparent = $userTable->editUser($buyerParentId);
				$buyerparentEmail=$buyerparent->email;
				$buyerparentName=$buyerparent->company_name;
				$buyerparentParent=$buyerparent->parent_user_id;
				//product owner details
				$productOwnerparentDetails = $userTable->editUser($buyerParentId);
				$productOwnerparentDetailsEmail=$productOwnerparentDetails->email;
				$productOwnerparentDetailsName=$productOwnerparentDetails->company_name;
				$productOwnerparentDetailsParent=$productOwnerparentDetails->parent_user_id;
				//super admin
				$superadmin = $userTable->editUser($buyerParentId);
				$superadmintEmail=$superadmin->email;
				$superadminName=$superadmin->company_name;				
			}			
		//superadmin
			$superadminMessage = str_replace("<FULLNAME>","$superadminName", $superadminMessage);
			$superadminMessage = str_replace("<ADMINUSERNAME>","$buyerparentName", $superadminMessage);
			$superadminMessage = str_replace("<BUYERNAME>","$buyerName", $superadminMessage);
			$superadminMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $superadminMessage);
			$superadminMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $superadminMessage);
			$superadminMessage = str_replace("<STATUS>","$ststus", $superadminMessage);
			$superadminMessage = str_replace("<MESSAGE>","$message", $superadminMessage);
			if(sendMail($superadmintEmail,$superadminSubject,$superadminMessage)){				
				//product owner parent	
				$productownerparentMessage = str_replace("<FULLNAME>","$buyerparentName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<BUYERNAME>","$buyerName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<STATUS>","$ststus", $productownerparentMessage);
				$productownerparentMessage = str_replace("<MESSAGE>","$message", $productownerparentMessage);
				if(sendMail($productOwnerparentDetailsEmail,$productownerparentSubject,$productownerparentMessage)){
					//product owner
					$changestatusproductownerMessage = str_replace("<STATUS>","$ststus", $changestatusproductownerMessage);
					$changestatusproductownerMessage = str_replace("<MESSAGE>","$message", $changestatusproductownerMessage);
					if(sendMail($productownerEmail,$changestatusproductownerSubject,$changestatusproductownerMessage)){
						//buyer parent
						$buyerparentMessage = str_replace("<FULLNAME>","$buyerparentName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<BUYERNAME>","$buyerName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<STATUS>","$ststus", $buyerparentMessage);
						$buyerparentMessage = str_replace("<MESSAGE>","$message", $buyerparentMessage);
						if(sendMail($buyerparentEmail,$buyerparentSubject,$buyerparentMessage)){
							//buyer 
							$changeStatusBuyerMessage = str_replace("<STATUS>","$ststus", $changeStatusBuyerMessage);
							$changeStatusBuyerMessage = str_replace("<MESSAGE>","$message", $changeStatusBuyerMessage);
							if(sendMail($buyerEmail,$changeStatusBuyerSubject,$changeStatusBuyerMessage)){
								$changeStatus= $this->getBuyTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus']);
								$changeStatus= $this->getHistoryTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus'],$_POST['type'],$_POST['note']);
								return $this->redirect()->toUrl('view-buy-detailes?product='.$user_product_id);			
							}
						}
					}
				}
			}
		}else if(isset($_GET['buy_id']) && $_GET['buy_id']!=""){
			$getbuy_info = $this->getBuyTable()->buylist($_GET['buy_id']);
			$user_details = $this->getBuyTable()->user_details($_GET['buy_id']);
			$status=$_GET['status'];	
			if($getbuy_info!=""){
				return new ViewModel(array(
					'buyDetails'     =>  $getbuy_info,				
					'user_details'   =>  $user_details,				
					'status'         =>  $status,				
				));
			}
		}
	}
	public function updateBidproductStatusAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');	
		global $changeStatusBuyerSubject;
		global $changeStatusBuyerMessage;
		global $buyerparentSubject;
		global $buyerparentMessage;		
		global $changestatusproductownerSubject;
		global $changestatusproductownerMessage;		
		global $productownerparentSubject;
		global $productownerparentMessage;
		global $superadminSubject;
		global $superadminMessage;
		if(isset($_POST['id']) && $_POST['id']!=""){
			$user_product_id=$_POST['hid_product_id'];
			$userTable = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$buyDetails = $userTable->editUser($_POST['buyer_id']);	
			if(isset($_POST['newStatus']) && $_POST['newStatus']!=""){
				if($_POST['newStatus']==0){
					$ststus='Pending';
				}else if($_POST['newStatus']==1){
					$ststus='Processing';
				}else if($_POST['newStatus']==2){
					$ststus='Processed';
				}else if($_POST['newStatus']==3){
					$ststus='Canceld';
				}	
			}
			$message=$_POST['note'];
			//Buyer Deatils
			$buyerEmail=$buyDetails->email;
			$buyerName=$buyDetails->company_name;
			$buyerParentId=$buyDetails->parent_user_id;
			$productOwnerDetails = $userTable->editUser($_POST['buyer_id']);
			//Product Owner Deatils
			$productownerEmail=$productOwnerDetails->email;
			$productOwnerName=$productOwnerDetails->company_name;
			$productownerParent=$productOwnerDetails->parent_user_id;
			if($buyerParentId!=$productownerParent){
				//product owner parent details 
				$productOwnerparentDetails = $userTable->editUser($productownerParent);
				$productOwnerparentDetailsEmail=$productOwnerparentDetails->email;
				$productOwnerparentDetailsName=$productOwnerparentDetails->company_name;
				$productOwnerparentDetailsParent=$productOwnerparentDetails->parent_user_id;
				//Buyer parent
				$buyerparent = $userTable->editUser($buyerParentId);
				$buyerparentEmail=$buyerparent->email;
				$buyerparentName=$buyerparent->company_name;
				$buyerparentParent=$buyerparent->parent_user_id;
				//super admin
				$superadmin = $userTable->editUser($buyerParentId);
				$superadmintEmail=$superadmin->email;
				$superadminName=$superadmin->company_name;				
			}else{
				//Buyer and product owner details
				$buyerparent = $userTable->editUser($buyerParentId);
				$buyerparentEmail=$buyerparent->email;
				$buyerparentName=$buyerparent->company_name;
				$buyerparentParent=$buyerparent->parent_user_id;
				//product owner details
				$productOwnerparentDetails = $userTable->editUser($buyerParentId);
				$productOwnerparentDetailsEmail=$productOwnerparentDetails->email;
				$productOwnerparentDetailsName=$productOwnerparentDetails->company_name;
				$productOwnerparentDetailsParent=$productOwnerparentDetails->parent_user_id;
				//super admin
				$superadmin = $userTable->editUser($buyerParentId);
				$superadmintEmail=$superadmin->email;
				$superadminName=$superadmin->company_name;				
			}			
		//superadmin
			$superadminMessage = str_replace("<FULLNAME>","$superadminName", $superadminMessage);
			$superadminMessage = str_replace("<ADMINUSERNAME>","$buyerparentName", $superadminMessage);
			$superadminMessage = str_replace("<BUYERNAME>","$buyerName", $superadminMessage);
			$superadminMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $superadminMessage);
			$superadminMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $superadminMessage);
			$superadminMessage = str_replace("<STATUS>","$ststus", $superadminMessage);
			$superadminMessage = str_replace("<MESSAGE>","$message", $superadminMessage);
			if(sendMail($superadmintEmail,$superadminSubject,$superadminMessage)){				
				//product owner parent	
				$productownerparentMessage = str_replace("<FULLNAME>","$buyerparentName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<BUYERNAME>","$buyerName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $productownerparentMessage);
				$productownerparentMessage = str_replace("<STATUS>","$ststus", $productownerparentMessage);
				$productownerparentMessage = str_replace("<MESSAGE>","$message", $productownerparentMessage);
				if(sendMail($productOwnerparentDetailsEmail,$productownerparentSubject,$productownerparentMessage)){
					//product owner
					$changestatusproductownerMessage = str_replace("<STATUS>","$ststus", $changestatusproductownerMessage);
					$changestatusproductownerMessage = str_replace("<MESSAGE>","$message", $changestatusproductownerMessage);
					if(sendMail($productownerEmail,$changestatusproductownerSubject,$changestatusproductownerMessage)){
						//buyer parent
						$buyerparentMessage = str_replace("<FULLNAME>","$buyerparentName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<BUYERNAME>","$buyerName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<PRODUCTOWNERPARENT>","$productOwnerparentDetailsName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<PRODUCTNAME>","$productOwnerName", $buyerparentMessage);
						$buyerparentMessage = str_replace("<STATUS>","$ststus", $buyerparentMessage);
						$buyerparentMessage = str_replace("<MESSAGE>","$message", $buyerparentMessage);
						if(sendMail($buyerparentEmail,$buyerparentSubject,$buyerparentMessage)){
							//buyer 
							$changeStatusBuyerMessage = str_replace("<STATUS>","$ststus", $changeStatusBuyerMessage);
							$changeStatusBuyerMessage = str_replace("<MESSAGE>","$message", $changeStatusBuyerMessage);
							if(sendMail($buyerEmail,$changeStatusBuyerSubject,$changeStatusBuyerMessage)){
								$changeStatus= $this->getBidTable()->changeStatusbidProduct($_POST['id'],$_POST['newStatus']);
								$changeStatus= $this->getHistoryTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus'],$_POST['type'],$_POST['note']);
								return $this->redirect()->toUrl('view-bid-detailes?product='.$user_product_id);			
							}
						}
					}
				}
			}
		}else if(isset($_GET['bid_id']) && $_GET['bid_id']!=""){
			$getbuy_info = $this->getBidTable()->bidlist($_GET['bid_id']);
			$user_details = $this->getBidTable()->user_details($_GET['bid_id']);
			$status=$_GET['status'];	
			if($getbuy_info!=""){
				return new ViewModel(array(
					'buyDetails'     =>  $getbuy_info,				
					'user_details'   =>  $user_details,				
					'status'         =>  $status,				
				));
			}
		}
	}
	public function addProductConfirmAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		global $productaddSubject;
		global $productaddMessage;
		global $productaddsuccessSubject;
		global $productaddsuccessMessage;		
		if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']=="" && $_POST['brand']!=''){
			if(isset($_POST['user_user_id']) && $_POST['user_user_id']!=""){
				$user_user_id=$_POST['user_user_id'];
			}else{
				$user_user_id=$_SESSION['user']['user_id'];
			}		
			$userDetails= $this->getusersnewTable()->editUser($user_user_id);
			$user_country=$userDetails->country;
			if($userDetails!=""){
				$parent_user_id=$userDetails->parent_user_id;
				$userDetails= $this->getusersnewTable()->editUser($parent_user_id);
				if($userDetails!=""){
					$superadmin_user_id=$userDetails->parent_user_id;
				}
			}			
			$get_product_id = $this->getProductsnewTable()->addProducts($_POST,$superadmin_user_id,$parent_user_id,$user_country);
			if($get_product_id!=""){
				$getproductDetails = $this->getProductsnewTable()->getProducts($get_product_id);
				if($getproductDetails!=""){					
				//productOwerDetails
					$productOwneremailid=$getproductDetails->email;
					$productOwnername=$getproductDetails->company_name;
					$productOwnerusertype=$getproductDetails->user_type;
				//end
					$productaddMessage = str_replace("<USERNAME>","$productOwnername", $productaddMessage);
					$productaddsuccessMessage = str_replace("<FULLNAME>","$productOwnername", $productaddsuccessMessage);
					if($getproductDetails->parent_user_id!=""){
						$get_user_info= $this->getusersnewTable()->editUser($getproductDetails->parent_user_id);						
						//Parent of product owner Details
							$parentUsername=$get_user_info->company_name;
							$parentEmail=$get_user_info->email;
							$parentUsertype=$get_user_info->user_type;
						//end
						$productaddMessage = str_replace("<FULLNAME>","$parentUsername", $productaddMessage);
						if(sendMail($productOwneremailid,$productaddsuccessSubject,$productaddsuccessMessage)){
							if(sendMail($parentEmail,$productaddSubject,$productaddMessage)){
								unset($_SESSION['products']);
								if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
									return $this->redirect()->toUrl('admin-product-list');
								}else{
									return $this->redirect()->toUrl('products-list');
								}
							}
						}	
					}else{
						if(sendMail($productOwneremailid,$productaddsuccessSubject,$productaddsuccessMessage)){
							unset($_SESSION['products']);
							if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
								return $this->redirect()->toUrl('admin-product-list');
							}else{
								return $this->redirect()->toUrl('products-list');
							}
						}
					}
				}							
			}	
		}else if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!="" && $_POST['brand']!=""){			
			global $changeProductSubject;
			global $changeProductMessage;			
			global $ownparentProductSubject;
			global $ownparentProductMessage;			
			global $subparentProductSubject;
			global $subparentProductMessage;
			global $mainsubparentProductSubject;
			global $mainsubparentProductMessage;
			global $mainparentProductSubject;
			global $mainparentProductMessage;	
			if(isset($_POST['user_user_id']) && $_POST['user_user_id']!=""){
				$user_user_id=$_POST['user_user_id'];
			}else{
				$user_user_id=$_SESSION['user']['user_id'];
			}		
			$userDetails= $this->getusersnewTable()->editUser($user_user_id);
			$user_country=$userDetails->country;
			if($userDetails!=""){
				$parent_user_id=$userDetails->parent_user_id;
				$userDetails= $this->getusersnewTable()->editUser($parent_user_id);
				if($userDetails!=""){
					$superadmin_user_id=$userDetails->parent_user_id;
				}
			}	
			$productid=$_POST['hid_product_id'];            		
			$p_userId=$_POST['hid_product_user_id'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($productid);
			if($getproductDetails!=""){
				if($getproductDetails->p_status!=$_POST['p_status']){
					//productOwerDetails
					$productOwneremailid=$getproductDetails->email;
					$productOwnername=$getproductDetails->company_name;
					$productOwnerusertype=$getproductDetails->user_type;
					$productName=$getproductDetails->genericname;
					if($_POST['p_status']=='0'){
						$changestatus='Pending';
					}else if($_POST['p_status']=='1'){
						$changestatus='Activate';
					}else if($_POST['p_status']=='2'){
						$changestatus='Deactivate';
					}
					//end					
					if($getproductDetails->parent_user_id!=""){
						$getdetails_parent= $this->getusersnewTable()->editUser($getproductDetails->parent_user_id);						
						//Parent about on product owner
							$parentUsername=$getdetails_parent->company_name;
							$parentEmail=$getdetails_parent->email;
							$parentUsertype=$getdetails_parent->user_type;
						//end					
						if($getproductDetails->parent_user_id!=$_SESSION['user']['user_id']){
							$getdetails_sub_parent= $this->getusersnewTable()->editUser($_SESSION['user']['user_id']);
							//Sub Parent Details
								$subparentUsername=$getdetails_sub_parent->company_name;
								$subparentEmail=$getdetails_sub_parent->email;
								$subparentUsertype=$getdetails_sub_parent->user_type;
							//end	
								if($getdetails_sub_parent->parent_user_id!=''){
									$getdetails_sub_parent_parent= $this->getusersnewTable()->editUser($getdetails_sub_parent->parent_user_id);
									//Sub parent of parent details
										$mainparentUsername=$getdetails_sub_parent_parent->company_name;
										$mainparentEmail=$getdetails_sub_parent_parent->email;
										$mainparentUsertype=$getdetails_sub_parent_parent->user_type;
									//end
									//Superadmin
									$mainsubparentProductMessage = str_replace("<FULLNAME>","$mainparentUsername", $mainsubparentProductMessage);
									$mainsubparentProductMessage = str_replace("<SUBPARENT>","$subparentUsername", $mainsubparentProductMessage);
									$mainsubparentProductMessage = str_replace("<PARENT>","$parentUsername", $mainsubparentProductMessage);
									$mainsubparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $mainsubparentProductMessage);
									$mainsubparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $mainsubparentProductMessage);
									$mainsubparentProductMessage = str_replace("<STATUS>","$changestatus", $mainsubparentProductMessage);
									//Sub Parentadmin
									$subparentProductMessage = str_replace("<FULLNAME>","$subparentUsername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PARENT>","$parentUsername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $subparentProductMessage);
									$subparentProductMessage = str_replace("<STATUS>","$changestatus", $subparentProductMessage);
									//parent on product owner
									$ownparentProductMessage = str_replace("<FULLNAME>","$parentUsername", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<STATUS>","$changestatus", $ownparentProductMessage);
									//product owner
									$changeProductMessage = str_replace("<FULLNAME>","$productOwnername", $changeProductMessage);
									$changeProductMessage = str_replace("<USERNAME>","$subparentUsername", $changeProductMessage);
									$changeProductMessage = str_replace("<PRODUCTNAME>","$productName", $changeProductMessage);
									$changeProductMessage = str_replace("<STATUS>","$changestatus", $changeProductMessage);
									//Superadmin
									if(sendMail($mainparentEmail,$mainsubparentProductSubject,$mainsubparentProductMessage)){
										//Sub Parentadmin										
										if(sendMail($subparentEmail,$subparentProductSubject,$subparentProductMessage)){
											//parent on product owner											
											if(sendMail($parentEmail,$ownparentProductSubject,$ownparentProductMessage)){
											//product owner												
												if(sendMail($productOwneremailid,$changeProductSubject,$changeProductMessage)){
												$get_product_id = $this->getProductsnewTable()->updateProducts($_POST,$productid,$p_userId,$parent_user_id,$superadmin_user_id,$user_country);
													if($get_product_id>=0){
														$countProducts = $this->getProductsnewTable()->getActivestatus($_SESSION['user']['user_id']);
														$_SESSION['user']['countProducts']=$countProducts;
														unset($_SESSION['products']);
														if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
															return $this->redirect()->toUrl('admin-product-list');
														}else{
															return $this->redirect()->toUrl('products-list');
														}
													}	
												}
											}
										}	
									}	
								}else{
									//Sub Parentadmin
									$subparentProductMessage = str_replace("<FULLNAME>","$subparentUsername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PARENT>","$parentUsername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $subparentProductMessage);
									$subparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $subparentProductMessage);
									$subparentProductMessage = str_replace("<STATUS>","$changestatus", $subparentProductMessage);
									//parent on product owner
									$ownparentProductMessage = str_replace("<FULLNAME>","$parentUsername", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $ownparentProductMessage);
									$ownparentProductMessage = str_replace("<STATUS>","$changestatus", $ownparentProductMessage);
									//product owner
									$changeProductMessage = str_replace("<FULLNAME>","$productOwnername", $changeProductMessage);
									$changeProductMessage = str_replace("<USERNAME>","$subparentUsername", $changeProductMessage);
									$changeProductMessage = str_replace("<PRODUCTNAME>","$productName", $changeProductMessage);
									$changeProductMessage = str_replace("<STATUS>","$changestatus", $changeProductMessage);
									if(sendMail($subparentEmail,$subparentProductSubject,$subparentProductMessage)){
										//parent on product owner
										if(sendMail($parentEmail,$ownparentProductSubject,$ownparentProductMessage)){
										//product owner
											if(sendMail($productOwneremailid,$changeProductSubject,$changeProductMessage)){
												$get_product_id = $this->getProductsnewTable()->updateProducts($_POST,$productid,$p_userId,$parent_user_id,$superadmin_user_id,$user_country);
												if($get_product_id>=0){
													$countProducts = $this->getProductsnewTable()->getActivestatus($_SESSION['user']['user_id']);
													$_SESSION['user']['countProducts']=$countProducts;
													unset($_SESSION['products']);
													if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
														return $this->redirect()->toUrl('admin-product-list');
													}else{
														return $this->redirect()->toUrl('products-list');
													}
												}	
											}
										}
									}
								}
						}else{	
							$getdetails_parent_parent= $this->getusersnewTable()->editUser($getproductDetails->parent_user_id);
							// parent of parent
								$mainparentUsername=$getdetails_parent_parent->company_name;
								$mainparentEmail=$getdetails_parent_parent->email;
								$mainparentUsertype=$getdetails_parent_parent->user_type;
							//end
							//Superadmin
							$mainparentProductMessage = str_replace("<FULLNAME>","$mainparentUsername", $mainparentProductMessage);
							$mainparentProductMessage = str_replace("<PARENT>","$parentUsername", $mainparentProductMessage);
							$mainparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $mainparentProductMessage);
							$mainparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $mainparentProductMessage);
							$mainparentProductMessage = str_replace("<STATUS>","$changestatus", $mainparentProductMessage);
							//Parent admin
							$ownparentProductMessage = str_replace("<FULLNAME>","$parentUsername", $ownparentProductMessage);
							$ownparentProductMessage = str_replace("<PRODUCTOWNER>","$productOwnername", $ownparentProductMessage);
							$ownparentProductMessage = str_replace("<PRODUCTNAME>","$productName", $ownparentProductMessage);
							$ownparentProductMessage = str_replace("<STATUS>","$changestatus", $ownparentProductMessage);
							//product owner
							$changeProductMessage = str_replace("<FULLNAME>","$productOwnername", $changeProductMessage);
							$changeProductMessage = str_replace("<USERNAME>","$mainparentUsername", $changeProductMessage);
							$changeProductMessage = str_replace("<PRODUCTNAME>","$productName", $changeProductMessage);
							$changeProductMessage = str_replace("<STATUS>","$changestatus", $changeProductMessage);
							if(sendMail($mainparentEmail,$mainparentProductSubject,$mainparentProductMessage)){
								//Parent admin
								if(sendMail($parentEmail,$ownparentProductSubject,$ownparentProductMessage)){
									//product owner									
									if(sendMail($productOwneremailid,$changeProductSubject,$changeProductMessage)){
										$get_product_id = $this->getProductsnewTable()->updateProducts($_POST,$productid,$p_userId,$parent_user_id,$superadmin_user_id,$user_country);
										if($get_product_id>=0){
											$countProducts = $this->getProductsnewTable()->getActivestatus($_SESSION['user']['user_id']);
											$_SESSION['user']['countProducts']=$countProducts;
											unset($_SESSION['products']);
											if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
												return $this->redirect()->toUrl('admin-product-list');
											}else{
												return $this->redirect()->toUrl('products-list');
											}
										}	
									}
								}	
							}
						}	
					}else{
						//Superadmin products
						$changeProductMessage = str_replace("<FULLNAME>","$productOwnername", $changeProductMessage);
						$changeProductMessage = str_replace("<USERNAME>","$productOwnername", $changeProductMessage);
						$changeProductMessage = str_replace("<PRODUCTNAME>","$productName", $changeProductMessage);
						$changeProductMessage = str_replace("<STATUS>","$changestatus", $changeProductMessage);
						if(sendMail($productOwneremailid,$changeProductSubject,$changeProductMessage)){
							$get_product_id = $this->getProductsnewTable()->updateProducts($_POST,$productid,$p_userId,$parent_user_id,$superadmin_user_id,$user_country);
							if($get_product_id>=0){
								$countProducts = $this->getProductsnewTable()->getActivestatus($_SESSION['user']['user_id']);
								$_SESSION['user']['countProducts']=$countProducts;
								unset($_SESSION['products']);
								if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
									return $this->redirect()->toUrl('admin-product-list');
								}else{
									return $this->redirect()->toUrl('products-list');
								}
							}	
						}
					}
				}else{
					$get_product_id = $this->getProductsnewTable()->updateProducts($_POST,$productid,$p_userId,$parent_user_id,$superadmin_user_id,$user_country);
					if($get_product_id>=0){
						$countProducts = $this->getProductsnewTable()->getActivestatus($_SESSION['user']['user_id']);
						$_SESSION['user']['countProducts']=$countProducts;
						unset($_SESSION['products']);
						if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
							return $this->redirect()->toUrl('admin-product-list');
						}else{
							return $this->redirect()->toUrl('products-list');
						}
					}	
				}
			}			
		}else if(isset($_GET['product_id']) && $_GET['product_id']!=""){
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();			
			$getallcategories = $this->getCategoriesTable()->getallCategories();
			$get_product_details = $this->getProductsnewTable()->getProducts($_GET['product_id']);
			return new ViewModel(array(
				'productsDeatils'   =>  $get_product_details,
				'getCountries'      =>  $getCountries,
				'getallcategories'  =>  $getallcategories,
			));
		}else{
			$getcountry= $this->getusersnewTable()->getcountries($_SESSION['user']['user_id']);
			$currency=$getcountry;
			if($getcountry!=""){
				return new ViewModel(array(
					'currency'   =>  $currency,				
				));
			}
		}
	}
	public function addProductNextAction(){	
		if(isset($_POST['changeprice']) && $_POST['changeprice']!=""){
			if(isset($_POST['user_id']) && $_POST['user_id']!=null){
				$user_id=explode("/",$_POST['user_id']);
				$getcountry= $this->getusersnewTable()->getcountries($user_id[0]);
				$currency=$getcountry->currency;
				$_SESSION['countryprice']=$currency;
				if($getcountry!=""){
					return new JSONModel(array(
						'currency'   =>  $currency,				
					));
				}
			}else{
				$currency='INR';
				$_SESSION['countryprice']=$currency;
				return new ViewModel(array(
					'currency'   =>  $currency,				
				));
			}		
		}else if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']=="" && $_POST['qty']!=''){
			$user_session = new Container('products');
			$user_session->selling=$_POST['selling'];
			$user_session->qty=$_POST['qty'];
			$user_session->units=$_POST['units'];
			$user_session->year=$_POST['year'];
			$user_session->packing=$_POST['packing'];
			$user_session->per=$_POST['per'];
			$user_session->tsh=$_POST['tsh'];
			$user_session->tshm=$_POST['tshm'];
			$user_session->type=$_POST['type'];
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!="superadmin"){
				$user_session->p_status=0;
			}else{
				$user_session->p_status=$_POST['p_status'];
			}
			$user_session->currency=$_POST['user_currency'];
			if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
				$product_id=$_POST['hid_product_id'];
				return $this->redirect()->toUrl('add-product-add-product-confirm?product_id='.$product_id);
			}else{
				return $this->redirect()->toUrl('add-product-confirm');
			}		
		}else if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!="" && $_POST['qty']!=""){
			$user_session = new Container('products');
			$user_session->selling=$_POST['selling'];
			$user_session->qty=$_POST['qty'];
			$user_session->units=$_POST['units'];
			$user_session->year=$_POST['year'];
			$user_session->packing=$_POST['packing'];
			$user_session->per=$_POST['per'];
			$user_session->tsh=$_POST['tsh'];
			$user_session->tshm=$_POST['tshm'];
			$user_session->type=$_POST['type'];
			$user_session->p_status=$_POST['p_status'];
			$user_session->currency=$_POST['user_currency'];
			$p_userId=$_POST['hid_product_user_id'];            		
			if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
				$product_id=$_POST['hid_product_id'];
				return $this->redirect()->toUrl('add-product-confirm?product_id='.$product_id);
			}else{
				return $this->redirect()->toUrl('add-product-confirm');
			}		
		}else if(isset($_GET['product_id']) && $_GET['product_id']!=""){
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();			
			$getallcategories = $this->getCategoriesTable()->getallCategories();
			$get_product_details = $this->getProductsnewTable()->getProducts($_GET['product_id']);
			return new ViewModel(array(
				'productsDeatils'   =>  $get_product_details,
				'getCountries'      =>  $getCountries,
				'getallcategories'  =>  $getallcategories,
			));
		}else{
			$getcountry= $this->getusersnewTable()->getcountries($_SESSION['user']['user_id']);
			$currency=$getcountry;
			if($getcountry!=""){
				return new ViewModel(array(
					'currency'   =>  $currency,				
				));
			}
		}
	}	
	public function addProductAction(){	
		if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']=="" && $_POST['brand']!=""){
			$user_session = new Container('products');
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$admindropdown=explode("/",$_POST['admindropdown']);
				$user_session->admin_user_id=$admindropdown[0];
				$user_session->admindropdown_name=$admindropdown[1];
			}else{
				$user_session->admin_user_id='';
			}
			$userdropdown=explode("/",$_POST['userdropdown']);
			$user_session->user_user_id=$userdropdown[0];
			$user_session->userdropdown_name=$userdropdown[1];
			$user_session->brand=$_POST['brand'];
			$user_session->generic=$_POST['generic'];
			$user_session->fuction=$_POST['fuction'];
			$user_session->manufacture=$_POST['manufacture'];
			$country=explode("/",$_POST['origin']);
			$user_session->origin=$country[0];
			$user_session->origin_name=$country[1];
			$category=explode("/",$_POST['category_id']);
			$user_session->category_id=$category[0];
			$user_session->category_name=$category[1];			
			if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
				$product_id=$_POST['hid_product_id'];
				return $this->redirect()->toUrl('add-product-next?product_id='.$product_id);
			}else{
				return $this->redirect()->toUrl('add-product-next');
			}				
		}else if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!="" && $_POST['brand']!=""){
			$user_session = new Container('products');
			$admindropdown=explode("/",$_POST['admindropdown']);
			$user_session->admin_user_id=$admindropdown[0];
			$user_session->admindropdown_name=$admindropdown[1];
			$userdropdown=explode("/",$_POST['userdropdown']);
			$user_session->user_user_id=$userdropdown[0];
			$user_session->userdropdown_name=$userdropdown[1];
			$user_session->brand=$_POST['brand'];
			$user_session->generic=$_POST['generic'];
			$user_session->fuction=$_POST['fuction'];
			$user_session->manufacture=$_POST['manufacture'];
			$country=explode("/",$_POST['origin']);			
			$user_session->origin=$country[0];
			$user_session->origin_name=$country[1];
			$category=explode("/",$_POST['category_id']);
			$user_session->category_id=$category[0];
			$user_session->category_name=$category[1];
			if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
				$product_id=$_POST['hid_product_id'];
				return $this->redirect()->toUrl('add-product-next?product_id='.$product_id);
			}else{
				return $this->redirect()->toUrl('add-product-next');
			}
		}else if(isset($_GET['product_id']) && $_GET['product_id']!=""){		
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();				
				$getallusers = '';
			}
			else{
				 $getalladmins='';
				 $admin_id=$_SESSION['user']['user_id'];
				 $getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);		
			}
			$get_product_details = $this->getProductsnewTable()->getProducts($_GET['product_id']);
			return new ViewModel(array(
				'productsDeatils' => $get_product_details,
				'getCountries'    => $getCountries,
				'getalladmins' 	  => $getalladmins,	
				'getallusers' 	=>  $getallusers,	
			));
		}else if(isset($_GET['is_first']) && $_GET['is_first']!=""){
			unset($_SESSION['products']);			
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();
				$getallusers = '';
			}
			else{
				 $getalladmins='';
				 $admin_id=$_SESSION['user']['user_id'];
				 $getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);		
			}
			$getCountries=$countrytable->getallcountries();
			return new ViewModel(array(
				'getCountries'  =>  $getCountries,
				'getalladmins' 	=>  $getalladmins,
				'getallusers' 	=>  $getallusers,
			));
		}else{	
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();
				$getallusers = '';
			}
			else{
				 $getalladmins='';
				 $admin_id=$_SESSION['user']['user_id'];
				 $getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);	
			}
			$getCountries=$countrytable->getallcountries();
			return new ViewModel(array(
				'getCountries'    =>  $getCountries,
				'getalladmins' 	  => $getalladmins,
				'getallusers' 	=>  $getallusers,
			));
		}
	}
	public function addCategoryAction(){
		if(isset($_POST['cat_name']) && $_POST['cat_name']!=""){
			$add_category = $this->getCategoriesTable()->addCategory($_POST);
			if($add_category>=0){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));	
			}else{
				$result = new JsonModel(array(					
					'output' => 'not success',
				));	
			}
			return $result;
		}else if(isset($_GET['cat_id']) && $_GET['cat_id']!=""){
			$get_category = $this->getCategoriesTable()->getallCategory($_GET['cat_id']);
			return new ViewModel(array(
				'get_category'    =>  $get_category,
			));
		}else{
			$viewModel = new ViewModel();
			return $viewModel;
		}
	}
	public function countryAction()
	{
	
	}
	public function industryAction(){
		if(isset($_POST['ind_name']) && $_POST['ind_name']!=""){
			$category='';
			foreach($_POST['catgories'] as $catgories){
				$category.=$catgories.',';
			}
			$industry=$this->getIndustryTable()->addindustry($_POST,rtrim($category,','));
			if(isset($_POST['allCountries'])){
				$getallcountries=$this->getCountryTable()->getallcountries();
				foreach($getallcountries as $country){
					$result=$this->getCountryAndIndustryTable()->addData($_POST,$industry,$country->country_id);
				}
			}else{
				foreach($_POST['country'] as $country){
					$result=$this->getCountryAndIndustryTable()->addData($_POST,$industry,$country);
				}
			}
			if($result>=0){
				return $this->redirect()->toUrl('all-industry-list');
			}
		}else if(isset($_GET['industry_id']) && $_GET['industry_id']!=""){
			$getallcountries=$this->getCountryTable()->getallcountries();
			$getindustrydetails=$this->getIndustryTable()->getindustryDetails($_GET['industry_id']);
             $getall_catgories = $this->getCategoriesTable()->getallCategories();
			   $result = new ViewModel(array(
					   'getindustrydetails' 	=> $getindustrydetails->buffer(),
					   'getallcategories'       => $getall_catgories->buffer(),
					   'getallcountries'  	    => $getallcountries->buffer(),
				));
			   return $result;
		}else{
			$getallcountries=$this->getCountryTable()->getallcountries();
			$getall_catgories = $this->getCategoriesTable()->getallCategories($_POST);
			$result = new ViewModel(array(
				'getallcategories'  	=> $getall_catgories,
				'getallcountries'  	    => $getallcountries,
			));
			return 	$result;
		}		
	}
	public function allCountriesAction(){
		$getallcountries=$this->getCountryTable()->getallcountries();
		$totalHtml ='<div id="TextBoxDiv'.$_POST["counter"].'" class="forms-list">';
		$totalHtml .= '<label class="control-label col-sm-3">Countries:</label>';
		$totalHtml .= '<div class="select_6 col-sm-9">';
		$totalHtml .= '<select id="country'.$_POST["counter"].'" name="country[]">'; 
		$totalHtml .= '<option value="0">Select</option>';
		foreach($getallcountries as $countries){
		$totalHtml .= '<option value="'.$countries->country_id.'">'.$countries->nicename.'</option>';
		}           
		$totalHtml .= '</select>';
		$totalHtml .= '</div><span id="addCountry'.$_POST["counter"].'"><a href="javascript:void(0);" onclick="addTextBox('.$_POST["counter"].')">&nbsp;&nbsp;&nbsp;Add</a></span>';
		$totalHtml .= '</div>';
		echo $totalHtml; exit;
	}
	public function allCategoriesAction(){
		$getall_catgories = $this->getCategoriesTable()->getallCategories();
		$totalHtml ='<div id="categoryBoxDiv'.$_POST["counter"].'" class="forms-list" style="padding:10px;">';
		$totalHtml .= '<label class="control-label col-sm-3">Categories:</label>';
		$totalHtml .= '<div class="select_6 col-sm-9">';
		$totalHtml .= '<select id="categories'.$_POST["counter"].'" name="catgories[]" class="form-control">'; 
		$totalHtml .= '<option value="0">Select</option>';
		foreach($getall_catgories as $catgories){
		$totalHtml .= '<option value="'.$catgories->category_id.'">'.$catgories->category_name.'</option>';
		}           
		$totalHtml .= '</select>';
		$totalHtml .= '</div><span id="addCategories'.$_POST["counter"].'"><a href="javascript:void(0);" onclick="addCategoryBox('.$_POST["counter"].')">&nbsp;&nbsp;&nbsp;Add</a></span>';
		$totalHtml .= '</div>';
		echo $totalHtml; exit;
	}
	public function getStatesAction(){	
		$html="";
		if(isset($_POST['countryid'])){
			$table = $this->getServiceLocator()->get('Application\Model\StateTable');
			$states=$table->getBasedcountry($_POST['countryid']);			
			$html.='<option value="">State</option>';
			foreach($states as $statename){
				$html.='<option value="'.$statename->state_id.'">'.$statename->state_name.'</option>';
			}			
			$result = new JsonModel(array(					
				'output' => 'success',
				'success'=>true,
				'statenames'=>$html,
				));
			return $result;
		}
	}
    public function salesAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];			
			$categoryids=array();$categories=array();
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getdetails = $table->editUser($userId);
			if($getdetails!=""){
				$industry_id=$getdetails->industry;
				$country_id=$getdetails->country;
				if($getdetails->user_type!='superadmin'){
					$industries = $this->getIndustryTable()->getindustry($industry_id);
					if(isset($industries->category_id)){
					  $categoryids=explode(",",$industries->category_id);
					  foreach($categoryids as $categoryvalue){
						 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryvalue);
						 $categories[]=$getallcategories;
					  }
					}
				}else{
					$categories = $this->getCategoriesTable()->getallCategories();
				}				
				if( $_SESSION['user']['user_type'] == 'user' ){
					$getallcountries=$this->getuserRegionsTable()->getuserregions($userId);
					$allindustries='';
				}else if( $_SESSION['user']['user_type'] == 'admin' ){
					$getallcountries=$this->getadminCombinationsTable()->getadmincountries($userId);
					$allindustries='';
				}else if( $_SESSION['user']['user_type'] == 'superadmin' ){
					$getallcountries=$this->getCountryTable()->getallcountries();
					$allindustries='';
				}
				return new ViewModel(array(
					'categories'  		 => $categories,					
					'industries'  		 => $allindustries,					
					'getallcountries'  		 => $getallcountries,	
				));
			}
		}	
	}
	public function buyingProductAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		global $buysendSubject;
		global $buysendMessage;
		if(isset($_GET['product']) && $_GET['product']!=""){
			$product_id=$_GET['product'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($product_id);
			if($getproductDetails!=""){
				$productOwneremailid=$getproductDetails->email;
				$username=$getproductDetails->company_name;				
				$buysendMessage = str_replace("<FULLNAME>","$username", $buysendMessage);
				$addBuyproduct=$this->getBuyTable()->addBuying($getproductDetails);
				if($addBuyproduct!=0){
					$getbuy_info = $this->getBuyTable()->buylist($addBuyproduct);
					$buyUser=$getbuy_info->company_name;
					$buysendMessage = str_replace("<USERNAME>","$buyUser", $buysendMessage);
					if(sendMail($productOwneremailid,$buysendSubject,$buysendMessage)){
						return $this->redirect()->toUrl('sales');
					}else{
						return $this->redirect()->toUrl('sales');
					}					
				}
			}
		}
	}
	public function bidProcesAction(){	
        include('public/PHPMailer_5.2.4/sendmail.php');
		global $bidsendSubject;
		global $bidsendMessage;
		global $bidsendparentSubject;
		global $bidsendparentMessage;
		global $bidderonproductSubject;
		global $bidderonproductMessage;
		if(isset($_POST['hidBidproduct']) && $_POST['hidBidproduct']!=""){
			$getproductDetails = $this->getProductsnewTable()->getProducts($_POST['hidBidproduct']);			
			if($getproductDetails!=""){
			//productOwerDetails
				$productOwneremailid=$getproductDetails->email;
				$productOwnername=$getproductDetails->company_name;
				$productOwnerusertype=$getproductDetails->user_type;
			//end			
				$bidsendMessage = str_replace("<FULLNAME>","$productOwnername", $bidsendMessage);					
				$addBid = $this->getBidTable()->addbiding($_POST,$getproductDetails,$_POST['ybprice']);
				$addBidproductHistory=$this->getHistoryTable()->addHistory($addBid,'bid');
				if($addBid!=0){				
					$getbid_info = $this->getBidTable()->bidlist($addBid);
			//Bidder Details
				$bidderName=$getbid_info->company_name;
				$bidderEmail=$getbid_info->email;
				$bidderUsertype=$getbid_info->user_type;
				$bidsendMessage = str_replace("<USERNAME>","$bidderName", $bidsendMessage);
			//end			
					if($getproductDetails->parent_user_id!=""){
						$get_user_info= $this->getusersnewTable()->editUser($getproductDetails->parent_user_id);						
					//Parent of product owner Details
						$parentUsername=$get_user_info->company_name;
						$parentEmail=$get_user_info->email;
						$parentUsertype=$get_user_info->user_type;
					//end	
						$bidsendparentMessage = str_replace("<FULLNAME>","$parentUserName", $bidsendparentMessage);
						$bidsendparentMessage = str_replace("<USERNAME>","$buyerName", $bidsendparentMessage);
						$bidsendparentMessage = str_replace("<BUSERNAME>","$productOwnername", $bidsendparentMessage);						
						$bidsendparentMessage = str_replace("<TYPE>","$productOwnerusertype", $bidsendparentMessage);
						//send to productOwner
						if(sendMail($productOwneremailid,$bidsendSubject,$bidsendMessage)){
							//send to parent
							if(sendMail($parentEmail,$bidsendparentSubject,$bidsendparentMessage)){
								//send to bidder
								if(sendMail($bidderEmail,$bidderonproductSubject,$bidderonproductMessage)){
									return $this->redirect()->toUrl('bid-proces?product='.$_POST['hidBidproduct'].'&success=success');
								}
							}
						}
					}else{
						if(sendMail($productOwneremailid,$bidsendSubject,$bidsendMessage)){						 
							if(sendMail($bidderEmail,$bidderonproductSubject,$bidderonproductMessage)){
									return $this->redirect()->toUrl('bid-proces?product='.$_POST['hidBidproduct'].'&success=success');
								}
						}else{							
							return $this->redirect()->toUrl('buy-proces?product='.$_POST['hidBidproduct'].'&success=success');
						}
					}
				}
			}
		}else if(isset($_GET['product']) && $_GET['product']!=""){
			$product_id=$_GET['product'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($product_id);
			$bidLists = $this->getBidTable()->bidlist($getproductDetails->product_id);
			$getalladmins = $this->getusersnewTable()->getalladmins();
		    if($getproductDetails!=""  || $bidLists!=""){
				return new ViewModel(array(
					'getproductDetails'  => $getproductDetails,				
					'bidLists'  => $bidLists,
					'getalladmins' 	=> $getalladmins,					
				));
			}
		}
	}
	public function buyProcesAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		global $buysendSubject;
		global $buysendMessage;
		global $buysendparentSubject;
		global $buysendparentMessage;
		global $buyeronproductSubject;
		global $buyeronproductMessage;
		if(isset($_POST['hidBuyproduct']) && $_POST['hidBuyproduct']!=""){			
			$user_id_user=explode("/",$_POST['userdropdown']);
			if($_POST['hidBuyproductType']=='Wholesale'){
				$quntity=0;
				$package=0;
				$per=0;
				$suggested_price=$_POST['acprice'];
				$changeProductDetails = $this->getProductsnewTable()->updateProductDetails($quntity,$package,$per,$_POST['hidBuyproduct'],$suggested_price);
			}else{
				$quntity=$_POST['quantity']-($_POST['userPacks']*$_POST['hidBuyproductPer']);
				$package=$quntity/$_POST['hidBuyproductPer'];
				$per=$_POST['hidBuyproductPer'];
				if($quntity==0){
					$suggested_price=$_POST['acprice'];
				}else{
					//$suggested_price=$_POST['acprice']-($_POST['userPacks']*$per);
					$suggested_price=$_POST['acprice'];
				}
				$changeProductDetails = $this->getProductsnewTable()->updateProductDetails($quntity,$package,$per,$_POST['hidBuyproduct'],$suggested_price);
			}
			$product_id=$_POST['hidBuyproduct'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($product_id);
			if($getproductDetails!=""){
			//productOwerDetails
				$productOwneremailid=$getproductDetails->email;
				$productOwnername=$getproductDetails->company_name;
				$productOwnerusertype=$getproductDetails->user_type;
			//end
				$buysendMessage = str_replace("<FULLNAME>","$productOwnername", $buysendMessage);				
				$addBuyproduct=$this->getBuyTable()->addBuying($user_id_user[0],$getproductDetails,$_POST);
				$addBuyproductHistory=$this->getHistoryTable()->addHistory($addBuyproduct,'buy');
				if($addBuyproduct!=0){
					$getbuy_info = $this->getBuyTable()->buylist($addBuyproduct);
			//Buyer Details
				$buyerName=$getbuy_info->company_name;
				$buyerEmail=$getbuy_info->email;
				$buyerUsertype=$getbuy_info->user_type;
			//end
				$buysendMessage = str_replace("<USERNAME>","$buyerName", $buysendMessage);
				$buyeronproductMessage = str_replace("<FULLNAME>","$buyerName", $buyeronproductMessage);
					if($getproductDetails->parent_user_id!=""){
						$get_user_info= $this->getusersnewTable()->editUser($getproductDetails->parent_user_id);						
					//Parent of product owner Details
						$parentUsername=$get_user_info->company_name;
						$parentEmail=$get_user_info->email;
						$parentUsertype=$get_user_info->user_type;
					//end						
						$buysendparentMessage = str_replace("<FULLNAME>","$parentUserName", $buysendparentMessage);
						$buysendparentMessage = str_replace("<USERNAME>","$buyerName", $buysendparentMessage);
						$buysendparentMessage = str_replace("<BUSERNAME>","$productOwnername", $buysendparentMessage);						
						$buysendparentMessage = str_replace("<TYPE>","$productOwnerusertype", $buysendparentMessage);
						//send to productOwner
						if(sendMail($productOwneremailid,$buysendSubject,$buysendMessage)){
							//send to parent
							if(sendMail($parentEmail,$buysendparentSubject,$buysendparentMessage)){
								//send to buyer
								if(sendMail($buyerEmail,$buyeronproductSubject,$buyeronproductMessage)){
									return $this->redirect()->toUrl('buy-proces?product='.$product_id.'&success=success');
								}
							}
						}
					}else{					
						$buyUser=$getbuy_info->company_name;
						$buysendMessage = str_replace("<USERNAME>","$buyUser", $buysendMessage);
						if(sendMail($productOwneremailid,$buysendSubject,$buysendMessage)){
							if(sendMail($buyerEmail,$buyeronproductSubject,$buyeronproductMessage)){
									return $this->redirect()->toUrl('buy-proces?product='.$product_id.'&success=success');
							}
						}else{
							return $this->redirect()->toUrl('buy-proces?product='.$product_id.'&success=success');
						}
                    }
				}
			}
		}else if(isset($_GET['product']) && $_GET['product']!=""){
			$product_id=$_GET['product'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($product_id);
			$buyLists = $this->getBuyTable()->buyproduct($getproductDetails->product_id);
			$getalladmins = $this->getusersnewTable()->getalladmins();
			if($getproductDetails!=""  || $bidLists!=""){
				return new ViewModel(array(
					'getproductDetails'  => $getproductDetails,				
					'buyLists'  => $buyLists,	
					'getalladmins' 	=> $getalladmins,					
				));
			}
		}	
	}
	public function getPricesOnProductAction(){
		if(isset($_POST['bid_product']) && $_POST['bid_product']!=""){	
			$bidingDetails = $this->getBidTable()->bidingDetails($_POST);
			if($bidingDetails!=""){
				$result= new JsonModel(array(
					'output'  	 => 'sucusses',
					'bid_price'  => $bidingDetails->biding_price,
				));
				return $result;
			}
		}
	}	
	public function addPriceOnProductAction(){
	   
	}
	public function adminProductListAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];
			$allusers=array();
			if($_SESSION['user']['user_type']=='admin'){
				$admin_users = $this->getusersnewTable()->getallAdminUsers();
				$getallcountries=$this->getCountryTable()->getallcountries();
					 foreach($admin_users as $users){
						$allusers[]=$users;
					 }
			}
			return new ViewModel(array(
				'allusers'  		 => $allusers,
			));		
		}
	}
	public function adminProductAjaxAction(){
	    $baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];
		
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];
		}
		if(isset($_POST['category_id']) && $_POST['category_id']!=""){
		   //superadmin
			if(isset($_SESSION['user']['user_type'])){
				if($_POST['user_id']!=""){
				}else{
					$_POST['user_id']=$_SESSION['user']['user_id'];
					$_POST['category_id']='all';
				}
				 if($_SESSION['user']['user_type']=="admin"){
						$categryid=$_POST['category_id'];
						$industryid=$_POST['industry_id'];
						$countryid=$_POST['country_id'];						
				//admin
					if($_POST['category_id']!="all"){		
						$user_type='';
						$product = $this->getProductsnewTable()->getalladminProducts($_POST['user_id'],$countryid,$industryid,$categryid,$type='',$user_type);
					}else{
						$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
						$getdetails = $table->editUser($_POST['user_id']);
						$cat_name='';$cat_total='';
						if($getdetails!=""){ 
							$user_type=$getdetails->user_type;
							$product = $this->getProductsnewTable()->getalladminProducts($_POST['user_id'],$countryid,$industryid,$categryid,$type='all',$user_type);
						}						
					}
					$data = array();$i=0;
					if(isset($product) && $product!=''){
						foreach($product as $value){
							if($value->p_status=='0'){
									$status='Pending';
							}else if($value->p_status=='1'){
									$status='Active';
							}else{
									$status='Inactive';
							}
							$id=$value->product_id;
							$data[$i]['sno']=$i+1;
							$data[$i]['genericname']= $value->genericname;
							$data[$i]['manufacturer'] =$value->manufacturer;
							if($value->quntity==0){
							$data[$i]['quntity']='Out of stock';
							}else{
							$data[$i]['quntity'] =$value->quntity;
							}
							$data[$i]['unites'] =$value->unites;
							$data[$i]['status'] =$status;
							$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/add-product?product_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>
							&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteProduct('.$id.')"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
							$i++;
						}
						$data['aaData'] = $data;
						print_r(json_encode($data['aaData']));exit;
					}else{
						echo '1';exit;
					}
				}					
			}	
		}
	}
	public function getCategoriesAjaxAction(){
		if(isset($_POST['user_id']) && $_POST['user_id']!=null){
			$categoryids=array();$categories=array();
			$userId=explode("/",$_POST['user_id']);
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getdetails = $table->editUser($userId[0]);
			if($getdetails!=""){
				$industry_id=$getdetails->industry;
				$country_id=$getdetails->country;
				$industries = $this->getIndustryTable()->getindustry($industry_id); 
				if($industries!=""){
				  $categoryids=explode(",",$industries->category_id);
				  foreach($categoryids as $categoryvalue){
					 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryvalue);
					 $categories[]=$getallcategories;
				  }
				}				
				$html="";
				$html.='<option value="all">Select a category</option>';
				foreach($categories as $category){	
					if(isset($_SESSION['products']['category_id']) && $_SESSION['products']['category_id']== $category->category_id ){
						$html.='<option value="'.$category->category_id.'/'.$category->category_name.'" selected="selected">'.$category->category_name.'</option>';
					}else if(isset($_POST['user_category_id']) && $_POST['user_category_id']== $category->category_id ){
						$html.='<option value="'.$category->category_id.'/'.$category->category_name.'" selected="selected">'.$category->category_name.'</option>';
					}else{
						$html.='<option value="'.$category->category_id.'/'.$category->category_name.'">'.$category->category_name.'</option>';
					}
				}
				return new JsonModel(array(
					'categories'  		 => $html,					
				));
			}
		}else{
			$html="";
			$html.='<option value="all">Select a category</option>';
			return new JsonModel(array(
					'categories'  		 => $html,					
				));
		}
	}
	public function productsListAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];			
			if( $_SESSION['user']['user_type'] == 'user' ){
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);
				$getallcountries=$getdetails->country;
				$allindustries='';				
			}else if( $_SESSION['user']['user_type'] == 'admin' ){
				$getallcountries=$this->getadminCombinationsTable()->getadmincountries($userId);
			}else if( $_SESSION['user']['user_type'] == 'superadmin' ){
				$allindustries = '';
				$getallcountries=$this->getCountryTable()->getallcountries();
			}
			return new ViewModel(array(
				'industries'  		 => $allindustries,					
				'getallcountries'  		 => $getallcountries,					
			));
		}
	}
	public function productAjaxAction(){
	    $baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];	
        if(isset($_POST['category_id']) && $_POST['category_id']!=""){
			//superadmin
			if(isset($_SESSION['user']['user_type'])){
				if($_SESSION['user']['user_type']=="superadmin"){					
					$product = $this->getProductsnewTable()->getallProducts1($_POST['country_id'],$_POST['industry_id'],$_POST['category_id']);
					$data = array();$i=0;
						if(isset($product) && $product!=''){
							foreach($product as $value){
								if($value->p_status=='0'){
										$status='Pending';
								}else if($value->p_status=='1'){
										$status='Active';
								}else if($value->p_status=='2'){
										$status='InActive';
								}
								$id=$value->product_id;
								$data[$i]['sno']=$i+1;
								$data[$i]['genericname']= $value->genericname;
								$data[$i]['manufacturer'] =$value->manufacturer;
								if($value->quntity==0){
									$data[$i]['quntity'] ='Out of stock';
								}else{
									$data[$i]['quntity'] =$value->quntity;
								}
								$data[$i]['unites'] =$value->unites;
								$data[$i]['saleing'] =$value->saleing;
								$data[$i]['status'] =$status;
								$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/add-product?product_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>
								&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteProduct('.$id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
								$i++;
							}
							$data['aaData'] = $data;
							print_r(json_encode($data['aaData']));exit;
						}else{
							echo '1';exit;
						}
				}else if($_SESSION['user']['user_type']=="user"){
				//user
					$product = $this->getProductsnewTable()->getallProducts1($_POST['country_id'],$_POST['industry_id'],$_POST['category_id']);
					$data = array();$i=0;
					if(isset($product) && $product!=''){
						foreach($product as $value){
							if($value->p_status=='0'){
									$status='Pending';
							}else if($value->p_status=='1'){
									$status='Active';
							}else if($value->p_status=='2'){
									$status='InActive';
							}
							$id=$value->product_id;
							$data[$i]['sno']=$i+1;
							$data[$i]['genericname']= $value->genericname;
							$data[$i]['manufacturer'] =$value->manufacturer;
							if($value->quntity==0){
									$data[$i]['quntity'] ='Out of stock';
								}else{
									$data[$i]['quntity'] =$value->quntity;
								}
							$data[$i]['unites'] =$value->unites;
							$data[$i]['saleing'] =$value->saleing;
							$data[$i]['status'] =$status;
							$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/add-product?product_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>
							&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/delete-products?product_id='.$id.'"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
							$i++;
						}
						$data['aaData'] = $data;
						print_r(json_encode($data['aaData']));exit;
					}else{
						echo '1';exit;
					}					
				}	
			}
		}else if(isset($_GET['check']) && $_GET['check']!=""){			
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!='admin'){
				$user_id=explode("/",$_GET['user_id']);	
				$admin_user_id=$user_id[0];
			}else{				
				$admin_user_id=$_SESSION['user']['user_id'];
			}
			$getadminUsers = $this->getusersnewTable()->getadminUsers($admin_user_id,$_GET['user_user_id'],$_GET['check']);
			$html="";
			foreach($getadminUsers as $user){	
				if(isset($_SESSION['products']['user_user_id']) && $_SESSION['products']['user_user_id']== $user->user_id ){
						$html.='<option value="'.$user->user_id.'/'.$user->company_name.'" selected="selected">'.$user->company_name.'</option>';
				}else if(isset($_GET['check']) && $_GET['check']== 'check' ){
					if(isset($_GET['user_user_id']) && $_GET['user_user_id']== $user->user_id ){
						$html.='<option value="'.$user->user_id.'/'.$user->company_name.'" selected="selected">'.$user->company_name.'</option>';
					}else{
						$html.='<option value="'.$user->user_id.'/'.$user->company_name.'">'.$user->company_name.'</option>';
					}
				}else{
					$html.='<option value="'.$user->user_id.'/'.$user->company_name.'">'.$user->company_name.'</option>';
				}
			}
			$result = new JsonModel(array(					
				'getadminUsers'		=>	$html,
			));	
			return $result;
		}
	}
	public function adminUsersAjaxAction(){
		if(isset($_POST['adminUserId']) && $_POST['adminUserId']!=""){
			$getadminUsers = $this->getusersnewTable()->getall_Admin_Users($_POST['adminUserId']);
			$html="";
			$html.='<option value="">Select a user</option>';
			foreach($getadminUsers as $user){	
			if(isset($_SESSION['products']['user_user_id']) && $_SESSION['products']['user_user_id']== $user->user_id ){
					$html.='<option value="'.$user->user_id.'/'.$user->company_name.'" selected="selected">'.$user->company_name.'</option>';
			}else if(isset($_POST['user_user_id']) && $_POST['user_user_id']== $user->user_id ){
					$html.='<option value="'.$user->user_id.'/'.$user->company_name.'" selected="selected">'.$user->company_name.'</option>';
			}else{
				$html.='<option value="'.$user->user_id.'/'.$user->company_name.'">'.$user->company_name.'</option>';
			}
			}
			$result = new JsonModel(array(					
				'getadminUsers'		=>	$html,
				'output'		=>	'success',
			));	
			return $result;
		}else if(isset($_POST['adminUserId']) && $_POST['adminUserId']=="all"){	
			$html="";
			$html.='<option value="all">Select a user</option>';
					$result = new JsonModel(array(					
				'getadminUsers'		=>	$html,
				'output'		=>	'success',
			));	
			return $result;
		}
	}
	public function allCategoriesListAction(){
	}
	public function allcategoriesAjaxAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];
	    $getallcategories = $this->getCategoriesTable()->getallCategories();
		$data = array();$i=0;
		if(isset($getallcategories) && $getallcategories!=''){
			foreach($getallcategories as $categories){
                $id=$categories->category_id;
				$data[$i]['sno']=$i+1;
				$data[$i]['category_name']= $categories->category_name;
				$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/add-category?cat_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteCategory('.$id.')"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
				$i++;
			}
		}
		$data['aaData'] = $data;
		print_r(json_encode($data['aaData']));exit;
	}
	public function allCountryListAction(){
	}
	public function allcountriesAjaxAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];
	     $getallcountries=$this->getCountryTable()->getallcountries();
		 $data = array();$i=0;
		 if(isset($getallcountries) && $getallcountries!=''){
			foreach($getallcountries as $countries){
                $id=$countries->country_id;
				$data[$i]['sno']=$i+1;
				$data[$i]['country_name']= $countries->country_name;
				$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/add-country?country_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteCountry('.$id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
				$i++;
			}
		}
		$data['aaData'] = $data;
		print_r(json_encode($data['aaData']));exit;
	}
	public function allIndustryListAction(){
	              
	}
	public function allindustriesAjaxAction(){
	    $baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];
	    $getallindustries=$this->getIndustryTable()->getallindustries();
		$i=0;$ind_stry=array();$cat_name='';$cat_total='';
		if(isset($getallindustries) && $getallindustries->count()!=0){
				foreach($getallindustries as $industries){
					if(isset($industries->category_id) && $industries->category_id!=""){
					 $array=explode(",",$industries->category_id);
					  foreach($array as $categoryId){
						 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryId);
						 $cat_name=$getallcategories->category_name;
						 $cat_total=$cat_total.$cat_name.",";
						 $cat_name="";
					  }
					  $categoryname=trim($cat_total,',');
					}
					$id=$industries->industry_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['category_name']=$categoryname;
					$data[$i]['industryname']= $industries->industryname;
					$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/industry?industry_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteIndustry('.$id.')"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
					$i++;
					$cat_total='';
				}	
			$data['aaData'] = $data;
			print_r(json_encode($data['aaData']));exit;
		}else{
			echo '1';exit;
		}
	}
	public function deleteProductsAction(){
		if(isset($_GET['product_id']) && $_GET['product_id']!=""){
			$product_id=$_GET['product_id'];
		    $deleteproduct = $this->getProductsnewTable()->deleteproducts($product_id);
			if($deleteproduct!=""){
				if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
				   return $this->redirect()->toUrl('admin-product-list');
				}else{
					return $this->redirect()->toUrl('products-list');
				}
			}
		}
	}
	public function salesProductsAjaxAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userid=$_SESSION['user']['user_id'];
		}else{
			$userid="";
		}
		$baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
		$categories='';
		if(isset($_POST['country_id']) && $_POST['country_id']!=""){		
			if($_POST['industry_id']!='all' && $_POST['category_id']='all'){
				$industries = $this->getIndustryTable()->getindustriesCategories($_POST['industry_id']);
				if(isset($industries->category_id)){
				  $categoryids=explode(",",$industries->category_id);
				  foreach($categoryids as $categoryvalue){
					 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryvalue);
					 $categories[]=$getallcategories;
				  }
				}
				if($categories[0]->category_id!=""){
					$product = $this->getProductsnewTable()->getallProducts($_POST['country_id'],$_POST['industry_id'],$categories[0]->category_id);
				}else{
					$product = $this->getProductsnewTable()->getallProducts($_POST['country_id'],$_POST['industry_id'],$_POST['category_id']);
				}
			}else{
				$product = $this->getProductsnewTable()->getallProducts($_POST['country_id'],$_POST['industry_id'],$_POST['category_id']);
			}
		}		
		$data = array();$i=0;
		if(isset($product) && $product!=''){
			foreach($product as $productsinfo){
				$id=$productsinfo->product_id;
				$data[$i]['sno']=$i+1;
				$data[$i]['brandname']= '<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/view-product?product_id='.$id.'">'.$productsinfo->brandcode.'</a>';
				$data[$i]['actualprice'] =$productsinfo->suggested_price;
				//$data[$i]['country'] =$productsinfo->user_currency;
				$data[$i]['currency'] =$productsinfo->user_currency;
				if($productsinfo->quntity==0){
					$data[$i]['quantity'] ='Out of Stock';
				}else{
					$data[$i]['quantity'] =$productsinfo->quntity;
				}
				$data[$i]['saleing'] =$productsinfo->saleing;
				/*if(isset($productsinfo->user_id) && $productsinfo->user_id=$userid){
					$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/add-product?product_id='.$id.'">View</a>';
				}else{*/
					if($productsinfo->quntity==0){
						$data[$i]['action'] ='Sold Out';
					}else{
						if(isset($productsinfo->type) && $productsinfo->type=="bid"){
						$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/bid-proces?product='.$id.'">Bid</a>/<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/buy-proces?product='.$id.'">Buy</a>';
						}else if(isset($productsinfo->type) && $productsinfo->type=="buy"){
						$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/buy-proces?product='.$id.'">Buy</a>';
						}
					}
					
				/*}*/
				$i++;
			}
		}
		$data['aaData'] = $data;
		print_r(json_encode($data['aaData']));exit;
	}
	public function viewProductAction(){
		if(isset($_GET['product_id']) && $_GET['product_id']!=""){
			$product_id=$_GET['product_id'];
			$getproductDetails = $this->getProductsnewTable()->getProducts($product_id);
			if($getproductDetails!=""){
				return new ViewModel(array(
					'productsDeatils'  => $getproductDetails,				
				));
			}
		}
	}
	public function bidOnProductsAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];			
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();				
				$getallusers = '';
				$getallcountries='';	
			}else if( $_SESSION['user']['user_type'] == 'admin' ){
				$getalladmins='';
				$admin_id=$_SESSION['user']['user_id'];
				$getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);
				$getallcountries='';				 
			}else if( $_SESSION['user']['user_type'] == 'user' ){
				$userId=$_SESSION['user']['user_id'];
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);				
				$getallcountries=$getdetails->country;
				$getalladmins='';
				$getallusers='';
			}
			return new ViewModel(array(
				'getallcountries'  	 => $getallcountries,					
				'getalladmin'  		 => $getalladmins,					
				'getallusers'  		 => $getallusers,					
			));				
		}
	}
	public function buyReceivedProductsAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];			
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();				
				$getallusers = '';
				$getallcountries='';	
			}else if( $_SESSION['user']['user_type'] == 'admin' ){
				$getalladmins='';
				$admin_id=$_SESSION['user']['user_id'];
				$getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);
				$getallcountries='';				 
			}else if( $_SESSION['user']['user_type'] == 'user' ){
				$userId=$_SESSION['user']['user_id'];
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);				
				$getallcountries=$getdetails->country;
				$getalladmins='';
				$getallusers='';
			}
			return new ViewModel(array(
				'getallcountries'  	 => $getallcountries,					
				'getalladmin'  		 => $getalladmins,					
				'getallusers'  		 => $getallusers,					
			));
		}
	}
	public function buyReceivedProductAjaxAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
		$adid=$_POST['admin_id'];
			$uid=$_POST['user_id'];
			$counid=$_POST['country_id'];
			$induid=$_POST['industry_id'];
			$catid=$_POST['category_id'];
		if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){	
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getProductsnewTable()->allbuyproducts();
			}else if($adid!='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$uid,$counid,$induid,$catid);				
			}else if($adid!='all' && $uid!='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adminId,$uid,$counid,$induid,$catid);	
			}else if($adid=='all' && $uid!='all' && $counid='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adminId,$user_id_user[0],$counid,$induid,$catid);			
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}	
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='user'){
			$user_id=$_SESSION['user']['user_id'];
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$buyprocess = $this->getProductsnewTable()->getallbuyProducts($adid,$user_id,$counid,$induid,$catid);
			}	
		}		
		if(isset($buyprocess) && $buyprocess!=""){
			   $data=array();$i=0;
				foreach($buyprocess as $buyproduct){
					$id=$buyproduct->product_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['genericname']=$buyproduct->genericname;
					if($buyproduct->quntity==0){
						$data[$i]['quntity'] ='Out of Stock';
					}else{
						$data[$i]['quntity'] =$buyproduct->quntity;
					}
					$data[$i]['suggested_price'] =$buyproduct->suggested_price;
					$data[$i]['saleing'] =$buyproduct->saleing;
					$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/view-buy-detailes?product='.$id.'">View buy details</a>';
					$i++;
				 }
			 $data['aaData'] = $data;
			 print_r(json_encode($data['aaData']));exit;
		}				
	}
	public function viewBuyDetailesAction(){
	
	
	}
	public function changeStatusOrdersAction(){
		if($_POST['type']=='buy'){
			$changeStatus= $this->getBuyTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus']);
		}else{
			$changeStatus= $this->getBidTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus']);
		}
		$changeStatus= $this->getHistoryTable()->changeStatusbuyProduct($_POST['id'],$_POST['newStatus'],$_POST['type']);
		 return new JsonModel(array(
			'output'  		 => 'success',										
		));
	}
	public function viewHistoryAction(){
		if(isset($_GET['product']) && $_GET['product']!=""){
			if($_GET['type']=='buy'){
				$history= $this->getBuyTable()->getDetailesOfProduct($_GET['product']);
			}else{
				$history= $this->getBidTable()->getDetailesOfProduct($_GET['product']);
			}
			 return new ViewModel(array(
				'history'  		 => $history,
				'type'  		 => $_GET['type'],				
			));
		 }	
	}
	public function buyDetailesAjaxAction()
    {
	     $baseUrls = $this->getServiceLocator()->get('config');
         $baseUrlArr = $baseUrls['urls'];
         $baseUrl = $baseUrlArr['baseUrl'];
         $basepath = $baseUrlArr['basePath'];
	     $buyproductDetails = $this->getBuyTable()->getbuyingDetails($_GET['buy_product']);
		if(isset($buyproductDetails) && $buyproductDetails!=""){
			$confirnm='';
			   $data=array();$i=0;
				foreach($buyproductDetails as $buyproduct){
				    $id=$buyproduct->product_id;
					if($buyproduct->order_status==0){
						$confirnm='Pending';
					}else if($buyproduct->order_status==1){
						$confirnm='Processing';
					}else if($buyproduct->order_status==2){
						$confirnm='Processed';
					}else if($buyproduct->order_status==3){
						$confirnm='Cancelled';
					}
					$html='<select id="status'.$buyproduct->buy_id.'" name="status'.$buyproduct->buy_id.'" onchange="changeStatus('.$buyproduct->buy_id.')">';
							if($confirnm=='Pending'){
								$html.='<option value="0" selected>Pending</option>';
							}else{
								$html.='<option value="0" >Pending</option>';
							}
							if($confirnm=='Processing'){
								$html.='<option value="1" selected>Processing</option>';
							}else{
								$html.='<option value="1" >Processing</option>';
							}
							if($confirnm=='Processed'){
								$html.='<option value="2" selected>Processed</option>';
							}else{
								$html.='<option value="2" >Processed</option>';
							}
							if($confirnm=='Cancelled'){
								$html.='<option value="3" selected>Cancelled</option>';
							}else{
								$html.='<option value="3" >Cancelled</option>';
							}
					$html.='</select>';
					$buy_id=$buyproduct->buy_id;
				    $data[$i]['sno']=$i+1;
					$data[$i]['companyname']=$buyproduct->company_name;
					$data[$i]['genericname']=$buyproduct->genericname;
					if($buyproduct->order_for_quantity==0){
						$data[$i]['order_for_quantity'] ='Out of Stock';
					}else{
					$data[$i]['order_for_quantity'] =$buyproduct->order_for_quantity;
					}
					$data[$i]['order_for_price'] =$buyproduct->order_for_price;
					$data[$i]['status'] =$html;
					//$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/all-buys-list?product='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>';
					$data[$i]['history'] ='<a href="javascript:void(0);" onclick="viewHistory('.$buy_id.')">history</a>';
					$i++;
				 }
			 $data['aaData'] = $data;
		     print_r(json_encode($data['aaData']));exit;
		}      
	}
	public function allBuysListAction(){
	    include('public/PHPMailer_5.2.4/sendmail.php');
		global $buyReceivedSubject;
		global $buyReceivedMessage;
		global $acceptenceonproductownerSubject;
		global $acceptenceonproductownerMessage;
		if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
			$confirm_buy = $this->getBuyTable()->statusOnBuy($_POST);
            if($confirm_buy>=0){
				$product_details = $this->getProductsnewTable()->getProducts($_POST['hid_product_id']);
				//Product Owner Details				
				$productOwner=$product_details->company_name;
				$productOwneremail=$product_details->email;
				$productname=$product_details->genericname;
				$acceptenceonproductownerMessage = str_replace("<FULLNAME>","$productOwner", $acceptenceonproductownerMessage);
				$buyDetails = $this->getBuyTable()->buyingDetails($_POST['hid_buy_id']);
				//Buyer Details
				$buyUser=$buyDetails->company_name;
				$buyerEmail=$buyDetails->email;				
				if($buyDetails->order_status==1){
					$confirm_status='acceptable';
				}else if($buyDetails->order_status==0){
					$confirm_status='pending';
				}
				$acceptenceonproductownerMessage = str_replace("<BUYERNAME>","$productOwner", $acceptenceonproductownerMessage);
				$acceptenceonproductownerMessage = str_replace("<PRODUCTNAME>","$productname", $acceptenceonproductownerMessage);
				$acceptenceonproductownerMessage = str_replace("<STATUS>","$confirm_status", $acceptenceonproductownerMessage);
				if($product_details!=""){
					$buyReceivedMessage = str_replace("<FULLNAME>","$buyUser", $buyReceivedMessage);
					$buyReceivedMessage = str_replace("<PRODUCTNAME>","$productname", $buyReceivedMessage);
					$buyReceivedMessage = str_replace("<STATUS>","$confirm_status",$buyReceivedMessage);
					//product owner
					if(sendMail($productOwneremail,$acceptenceonproductownerSubject,$acceptenceonproductownerMessage)){
						//buyer 
						if(sendMail($buyerEmail,$buyReceivedSubject,$buyReceivedMessage)){
							return $this->redirect()->toUrl('all-buys-list?product='.$_POST['hid_product_id'].'&success=success');
						}
					}
				}				
			}
		}else if(isset($_GET['product']) && $_GET['product']!=""){
			$allbuyslist = $this->getBuyTable()->getbuyinglist($_GET['product']);
			$result= new ViewModel(array(
					'allbuyslist'  =>$allbuyslist,
				  ));
		   return $result;
		}
	}
	
	
	public function bidprocessAjaxAction(){
	    $baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
		$adid=$_POST['admin_id'];
			$uid=$_POST['user_id'];
			$counid=$_POST['country_id'];
			$induid=$_POST['industry_id'];
			$catid=$_POST['category_id'];
		if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){	
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$bidprocess = $this->getProductsnewTable()->getallbidProducts();			
			}else if($adid!='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$uid,$counid,$induid,$catid);				
			}else if($adid!='all' && $uid!='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id_user[0],$counid,$induid,$catid);
			}
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adminId,$uid,$counid,$induid,$catid);	
			}else if($adid=='all' && $uid!='all' && $counid='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adminId,$user_id_user[0],$counid,$induid,$catid);			
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$adminId=$_SESSION['user']['user_id'];
				$user_id_user=explode("/",$uid);
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adminId,$user_id_user[0],$counid,$induid,$catid);
			}	
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='user'){
			$user_id=$_SESSION['user']['user_id'];
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id,$counid,$induid,$catid);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$bidprocess = $this->getProductsnewTable()->allbidProducts($adid,$user_id,$counid,$induid,$catid);
			}	
		}
		if(isset($bidprocess) && $bidprocess!=""){
			   $data=array();$i=0;
				foreach($bidprocess as $bidproduct){
					$id=$bidproduct->product_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['genericname']=$bidproduct->genericname;
					if($bidproduct->quntity==0){
						$data[$i]['quntity'] = 'Out of stock';
					}else{
						$data[$i]['quntity'] =$bidproduct->quntity;
					}
					$data[$i]['suggested_price'] =$bidproduct->suggested_price;
					$data[$i]['saleing'] =$bidproduct->saleing;
					$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/view-bid-detailes?product='.$id.'">View bid details</a>';
					$i++;
				 }
			 $data['aaData'] = $data;
			 print_r(json_encode($data['aaData']));exit;
		}
	}
	public function  viewBidDetailesAction()
    {
	 
	}
	public function bidDetailesAjaxAction()
    {
	     $baseUrls = $this->getServiceLocator()->get('config');
         $baseUrlArr = $baseUrls['urls'];
         $baseUrl = $baseUrlArr['baseUrl'];
         $basepath = $baseUrlArr['basePath'];
	     $bidproductDetails = $this->getBidTable()->getbidingDetails($_GET['bid_product']);
	     if(isset($bidproductDetails)&& $bidproductDetails!=""){
			   $data=array();$i=0;$confirnm='';
				foreach($bidproductDetails as $bidding_product){
				    $id=$bidding_product->product_id;
					if($bidding_product->quntity==0){
						$confirnm='Cancelled';
						$changeStatus= $this->getHistoryTable()->changeStatusbuyProduct($bidding_product->bid_id,'3','bid');
					}else if($bidding_product->biding_quantity>$bidding_product->quntity){
						$confirnm='Cancelled';
						$changeStatus= $this->getHistoryTable()->changeStatusbuyProduct($bidding_product->bid_id,'3','bid');
					}else{
						if($bidding_product->bid_status==0){
							$confirnm='Pending';
						}else if($bidding_product->bid_status==1){
							$confirnm='Processing';
						}else if($bidding_product->bid_status==2){
							$confirnm='Processed';
						}else if($bidding_product->bid_status==3){
							$confirnm='Cancelled';
						}
					}
					$html='<select id="status'.$bidding_product->bid_id.'" name="status'.$bidding_product->bid_id.'" onchange="changeStatus('.$bidding_product->bid_id.')">';
							if($confirnm=='Pending'){
								$html.='<option value="0" selected>Pending</option>';
							}else{
								$html.='<option value="0" >Pending</option>';
							}
							if($confirnm=='Processing'){
								$html.='<option value="1" selected>Processing</option>';
							}else{
								$html.='<option value="1" >Processing</option>';
							}
							if($confirnm=='Processed'){
								$html.='<option value="2" selected>Processed</option>';
							}else{
								$html.='<option value="2" >Processed</option>';
							}
							if($confirnm=='Cancelled'){
								$html.='<option value="3" selected>Cancelled</option>';
							}else{
								$html.='<option value="3" >Cancelled</option>';
							}
					$html.='</select>';
				    $bid_id=$bidding_product->bid_id;
				    $data[$i]['sno']=$i+1;
					$data[$i]['companyname']=$bidding_product->company_name;
					$data[$i]['genericname']=$bidding_product->genericname;
					$data[$i]['biding_price'] =$bidding_product->biding_price;
					$data[$i]['status'] =$html;
					//$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/all-bids-list?product='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>';
					$data[$i]['history'] ='<a href="javascript:void(0);" onclick="viewHistory('.$bid_id.')">history</a>';
					$i++;
				 }
			 $data['aaData'] = $data;
		     print_r(json_encode($data['aaData']));exit;
		}     
	}
	public function allBidsListAction()
	{
		include('public/PHPMailer_5.2.4/sendmail.php');
		global $confirmstatusonbidSubject;
		global $confirmstatusonbidMessage;
		if(isset($_POST['hid_product_id']) && $_POST['hid_product_id']!=""){
			$confirm_bid = $this->getBidTable()->statusOnBid($_POST);			
			if($confirm_bid>=0){
				$product_details = $this->getProductsnewTable()->getProducts($_POST['hid_product_id']);
				$bidDetails = $this->getBidTable()->bidDetails($_POST['hid_bid_id']);
				$bidUser=$bidDetails->company_name;
				if($bidDetails->bid_status=1){
					$confirm_status='acceptable';
				}else if($bidDetails->bid_status=0){
					$confirm_status='not acceptable';
				}
				$sentemail=$bidDetails->email;
				if($product_details!=""){
					$username=$product_details->company_name;
					$productname=$product_details->genericname;
					$confirmstatusonbidMessage = str_replace("<FULLNAME>","$bidUser", $confirmstatusonbidMessage);
					$confirmstatusonbidMessage = str_replace("<PRODUCTNAME>","$productname", $confirmstatusonbidMessage);
					$confirmstatusonbidMessage = str_replace("<STATUS>","$confirm_status", $confirmstatusonbidMessage);
					if(sendMail($sentemail,$confirmstatusonbidSubject,$confirmstatusonbidMessage)){
						$bidrecievedcount = $this->getBidTable()->bidrecivedCount($_SESSION['user']['user_id']);
						if($bidrecievedcount>=0){
							$_SESSION['user']['countBidrecived']=$bidrecievedcount;
							return $this->redirect()->toUrl('all-bids-list?product='.$_POST['hid_product_id'].'&success=success');
						}
					}
				}				
			}
		}else if(isset($_GET['product']) && $_GET['product']!=""){
			$allbidslist = $this->getBidTable()->getbidinglist($_GET['product']);
			$result= new ViewModel(array(
					'allbidslist'  =>$allbidslist,
				  ));
		   return $result;
		}
	}
	public function bidSentAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];			
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();				
				$getallusers = '';
				$getallcountries='';	
			}else if( $_SESSION['user']['user_type'] == 'admin' ){
				$getalladmins='';
				$admin_id=$_SESSION['user']['user_id'];
				$getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);
				$getallcountries='';				 
			}else if( $_SESSION['user']['user_type'] == 'user' ){
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);				
				$getallcountries=$getdetails->country;
				$getalladmins='';
				$getallusers='';
			}
			return new ViewModel(array(
				'getallcountries'  	 => $getallcountries,					
				'getalladmin'  		 => $getalladmins,					
				'getallusers'  		 => $getallusers,					
			));			
		}
	}
	public function bidSentAjaxAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$baseUrls = $this->getServiceLocator()->get('config');
			$baseUrlArr = $baseUrls['urls'];
			$baseUrl = $baseUrlArr['baseUrl'];
			$adid=$_POST['admin_id'];
			$uid=$_POST['user_id'];
			$counid=$_POST['country_id'];
			$induid=$_POST['industry_id'];
			$catid=$_POST['category_id'];
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){	
				if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){					
					$exitingBids = $this->getBidTable()->allbids();
				}else if($adid!='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
					$getalladmins = $this->getusersnewTable()->getall_Admin_Users($adid);
					$getalladmins->buffer();
					$allusers=array();
					$i=0;
					foreach($getalladmins as $adminusers){					
						$allusers[$i]=$adminusers->user_id;
						$i++;
					}
					$exitingBids = $this->getBidTable()->existingBidsOnusersProducts($allusers);			
				}else if($adid!='all' && $uid!='all' && $counid=='all' && $induid=='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);	
				}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}
			}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
				if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
					$getalladmins = $this->getusersnewTable()->getall_Admin_Users($_SESSION['user']['user_id']);
					$getalladmins->buffer();
					$allusers=array();
					$i=0;
					foreach($getalladmins as $adminusers){					
						$allusers[$i]=$adminusers->user_id;
						$i++;
					}
					$exitingBids = $this->getBidTable()->existingBidsOnusersProducts($allusers);	
				}else if($adid=='all' && $uid!='all' && $counid='all' && $induid=='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);				
				}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
					$user_id_user=explode("/",$uid);
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id_user[0],$catid);
				}	
			}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='user'){
				$user_id=$_SESSION['user']['user_id'];
				if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id,$catid);
				}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid=='all' && $catid=='all'){
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id,$catid);
				}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid=='all'){
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id,$catid);
				}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid!='all'){
					$exitingBids = $this->getBidTable()->existingBidsOnProducts($user_id,$catid);				
				}	
			}			
			if(isset($exitingBids)&& $exitingBids!=""){
			    $data=array();$i=0;
				foreach($exitingBids as $bidslist){
					$id=$bidslist->product_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['genericname']=$bidslist->genericname;
					if($bidslist->biding_quantity!=0){
					$data[$i]['quntity'] =$bidslist->biding_quantity;
					}else{
					$data[$i]['quntity'] ='Out of stock';
					}
					$data[$i]['suggested_price'] =$bidslist->actual_price;
					$data[$i]['saleing'] =$bidslist->saleing;
					$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/products/status-bids-list?product='.$id.'">View Bids</a>';
					$i++;
				 }
				$data['aaData'] = $data;
				print_r(json_encode($data['aaData']));exit;
			} 
		}
	}
	public function statusBidsListAction(){
	}
	public function statusBidsListAjaxAction(){
		if(isset($_GET['product']) && $_GET['product']!=""){
			$baseUrls = $this->getServiceLocator()->get('config');
			$baseUrlArr = $baseUrls['urls'];
			$baseUrl = $baseUrlArr['baseUrl'];
			$bidproductDetails = $this->getBidTable()->statusExstingbids($_GET['product']);
			if(isset($bidproductDetails)&& $bidproductDetails!=""){
				   $data=array();$i=0;
					foreach($bidproductDetails as $bidding_product){ 
						$id=$bidding_product->product_id;
						$data[$i]['sno']=$i+1;
						$data[$i]['username']=$bidding_product->company_name;
						$data[$i]['productname']=$bidding_product->genericname;
						$data[$i]['bidprice'] =$bidding_product->biding_price;
						if($bidding_product->quntity==0){
							$data[$i]['status']='Cancelled';
						}else if($bidding_product->biding_quantity>$bidding_product->quntity){
							$data[$i]['status']='Cancelled';
						}else{
							if($bidding_product->bid_status==0){
								$data[$i]['status']='Pending';
							}else if($bidding_product->bid_status==1){
								$data[$i]['status']='Processing';
							}else if($bidding_product->bid_status==2){
								$data[$i]['status']='Processed';
							}else if($bidding_product->bid_status==3){
								$data[$i]['status']='Cancelled';
							}
						}
						$i++;
					 }
				 $data['aaData'] = $data;
				 print_r(json_encode($data['aaData']));exit;
			}
		}	
	}
	public function reportsListAction(){
	
	}
	public function reportsPendingAction(){
		$total=0;$json='[["admin", "count"],';
		$final='';$cond=0;
		$adminlist = $this->getusersnewTable()->reportsData();
		if(isset($adminlist) && $adminlist!=''){
			foreach($adminlist as $admin){
				$userlist = $this->getusersnewTable()->getadminUsers($admin->user_id);
				foreach($userlist as $user){
					if(isset($_GET['id']) && $_GET['id']=='Pending Products'){
						$count = $this->getProductsnewTable()->getstatus($user->user_id);
						$total=$total+$count;
						$cond=$cond+$count;
					}else if(isset($_GET['id']) && $_GET['id']=='Active Products'){
						$count = $this->getProductsnewTable()->getActivestatus($user->user_id);
						$total=$total+$count;
						$cond=$cond+$count;
					}else if(isset($_GET['id']) && $_GET['id']=='Bid Products'){
						$count = $this->getBidTable()->getPending($user->user_id);
						$total=$total+$count;
						$cond=$cond+$count;
					}
				}
				$final=$final.'["'.$admin->company_name.'",'.$total.'],';
				$total=0;
			}
			if($cond!=0){
				$final=rtrim($final,',');
				$json.=$final.']'; 
				print_r($json);exit;
			}else{
				echo '1';exit;
			}
		}
	}
	public function BuyOnProductsAction(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
				$getalladmins = $this->getusersnewTable()->getalladmins();				
				$getallusers = '';
				$getallcountries='';	
			}else if( $_SESSION['user']['user_type'] == 'admin' ){
				$getalladmins='';
				$admin_id=$_SESSION['user']['user_id'];
				$getallusers = $this->getusersnewTable()->getall_Admin_Users($admin_id);
				$getallcountries='';				 
			}else if( $_SESSION['user']['user_type'] == 'user' ){
				$userId=$_SESSION['user']['user_id'];
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);				
				$getallcountries=$getdetails->country;	
				$getalladmins='';
				$getallusers='';
			}
			return new ViewModel(array(
				'getallcountries'  	 => $getallcountries,					
				'getalladmin'  		 => $getalladmins,					
				'getallusers'  		 => $getallusers,					
			));
		}
	}
	public function buyProductAjaxAction(){
	    $baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
        $basepath = $baseUrlArr['basePath'];
			$adid=$_POST['admin_id'];
			$uid=$_POST['user_id'];
			$counid=$_POST['country_id'];
			$induid=$_POST['industry_id'];
			$catid=$_POST['category_id'];
		if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){	
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getBuyTable()->allbuyProducts();			
			}else if($adid!='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$getalladmins = $this->getusersnewTable()->getall_Admin_Users($adid);
				$getalladmins->buffer();
				$allusers=array();
				$i=0;
				foreach($getalladmins as $adminusers){					
					$allusers[$i]=$adminusers->user_id;
					$i++;
				}				
				$buyprocess = $this->getBuyTable()->getalladminuserboughtproducts($allusers);				
			}else if($adid!='all' && $uid!='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id_user[0]);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id_user[0]);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id_user[0]);
			}else if($adid!='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->getallbuyProducts($user_id_user[0],$catid);
			}
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$getalladmins = $this->getusersnewTable()->getall_Admin_Users($_SESSION['user']['user_id']);
				$getalladmins->buffer();
				$allusers=array();
				$i=0;
				foreach($getalladmins as $adminusers){					
					$allusers[$i]=$adminusers->user_id;
					$i++;
				}				
				$buyprocess = $this->getBuyTable()->getalladminuserboughtproducts($allusers);				
			}else if($adid=='all' && $uid!='all' && $counid='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id_user[0]);				
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id_user[0]);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->getallbuyProducts($user_id_user[0],$catid);
			}else if($adid=='all' && $uid!='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$user_id_user=explode("/",$uid);
				$buyprocess = $this->getBuyTable()->getallbuyProducts($user_id_user[0],$catid);
			}	
		}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='user'){
			$user_id=$_SESSION['user']['user_id'];
			if($adid=='all' && $uid=='all' && $counid=='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid=='all' && $catid=='all'){
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid=='all'){
				$buyprocess = $this->getBuyTable()->alladminuserbuyproducts($user_id);
			}else if($adid=='all' && $uid=='all' && $counid!='all' && $induid!='all' && $catid!='all'){
				$buyprocess = $this->getBuyTable()->getallbuyProducts($user_id,$catid);
			}	
		}		
		if(isset($buyprocess) && $buyprocess!=""){
		     $data=array();$i=0;
			    foreach($buyprocess as $buyproduct){
				    $id=$buyproduct->product_id;
					if($buyproduct->order_status==0){
						$confirnm='Pending';
					}else if($buyproduct->order_status==1){
						$confirnm='Processing';
					}else if($buyproduct->order_status==2){
						$confirnm='Processed';
					}else if($buyproduct->order_status==3){
						$confirnm='Canceld';
					}
					$buy_id=$buyproduct->buy_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['genericname']=$buyproduct->genericname;
					if($buyproduct->order_for_quantity==0){
					$data[$i]['order_for_quantity'] ='Out of stock';
					}else{
					$data[$i]['order_for_quantity'] =$buyproduct->order_for_quantity;
					}
					$data[$i]['order_for_price'] =$buyproduct->order_for_price;
					$data[$i]['saleing'] =$buyproduct->saleing;
					$data[$i]['status'] =$confirnm;
					$data[$i]['action']='<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onClick="deleteBuy('.$buy_id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
					$i++;
				 }
			 $data['aaData'] = $data;
		     print_r(json_encode($data['aaData']));exit;
		}      
	}
	public function deleteBuyProductAction(){
		 if(isset($_GET['product']) && $_GET['product']!=""){
			 // $deleteproduct= $this->getBuyTable()->deletebuyProduct($_GET['product']);
			 $deleteproduct = $this->getHistoryTable()->deletebuyProduct($_GET['product']);
			 if($deleteproduct!=""){			      
				return $this->redirect()->toUrl('buy-on-products');
			 }
		 }	 
	}
	public function deleteCategoryAction(){
		if(isset($_GET['category_id']) && $_GET['category_id']!=""){
			$deletecategories = $this->getCategoriesTable()->deleteCategory($_GET['category_id']);
			if($deletecategories!=0){
				return $this->redirect()->toUrl('all-categories-list');
			}
		}	
	}
	public function deleteIndustryAction(){
		if(isset($_GET['industry_id']) && $_GET['industry_id']!=""){
		    $deleteindustries=$this->getIndustryTable()->deleteIndustry($_GET['industry_id']);
			if($deleteindustries!=0){
				return $this->redirect()->toUrl('all-industry-list');
			}
		}		
	}
	public function deleteCountryAction(){
		if(isset($_GET['country_id']) && $_GET['country_id']!=""){
			$deletecountries=$this->getCountryTable()->deleteCountry($_GET['country_id']);
			if($deletecountries!=0){
				return $this->redirect()->toUrl('all-country-list');
			}
		}		
	}	
	public function getusersnewTable()
	{
		if (!$this->usersnewTable) {
			$sm = $this->getServiceLocator();
			$this->usersnewTable = $sm->get('Users\Model\UsersnewTable');
		}
		return $this->usersnewTable;
	}
	public function getadminCombinationsTable()
	{
		if (!$this->adminCombinationsTable) {
			$sm = $this->getServiceLocator();
			$this->adminCombinationsTable = $sm->get('Users\Model\AdminCombinationsTable');
		}
		return $this->adminCombinationsTable;
	}	
	public function getuserRegionsTable()
	{
		if (!$this->userRegionsTable) {
			$sm = $this->getServiceLocator();
			$this->userRegionsTable = $sm->get('Users\Model\UserRegionsTable');
		}
		return $this->userRegionsTable;
	}
	public function getIndustryTable()
    {
        if (!$this->IndustryTable) {				
            $sm = $this->getServiceLocator();
            $this->IndustryTable = $sm->get('Products\Model\industryTable');			
        }
        return $this->IndustryTable;
    }
	public function getCategoriesTable()
    {
        if (!$this->categoriesTable) {				
            $sm = $this->getServiceLocator();
            $this->categoriesTable = $sm->get('Products\Model\CategoriesTable');			
        }
        return $this->categoriesTable;
    }
	public function getProductsTable()
    {
        if (!$this->productsTable) {				
            $sm = $this->getServiceLocator();
            $this->productsTable = $sm->get('Products\Model\ProductsTable');			
        }
        return $this->productsTable;
    }
	public function getProductsnewTable()
    {
        if (!$this->productsnewTable) {				
            $sm = $this->getServiceLocator();
            $this->productsnewTable = $sm->get('Products\Model\ProductsnewTable');			
        }
        return $this->productsnewTable;
    }
	public function getOffersTable()
    {
        if (!$this->offersTable) {
            $sm = $this->getServiceLocator();
            $this->offersTable = $sm->get('Products\Model\OffersTable');
        }
        return $this->offersTable;
    }
	public function getBuyTable()
    {
        if (!$this->buyTable) {
            $sm = $this->getServiceLocator();
            $this->buyTable = $sm->get('Products\Model\BuyTable');
        }
        return $this->buyTable;
    }
	public function getHistoryTable()
    {
        if (!$this->historyTable) {
            $sm = $this->getServiceLocator();
            $this->historyTable = $sm->get('Products\Model\HistoryTable');
        }
        return $this->historyTable;
    }
	public function getCountryAndIndustryTable()
    {
        if (!$this->countryAndIndustryTable) {
            $sm = $this->getServiceLocator();
            $this->countryAndIndustryTable = $sm->get('Products\Model\CountryAndIndustryTable');
        }
        return $this->countryAndIndustryTable;
    }
	public function getBidTable()
    {
        if (!$this->bidTable) {
            $sm = $this->getServiceLocator();
            $this->bidTable = $sm->get('Products\Model\BidTable');
        }
        return $this->bidTable;
    }	
	public function getUserTable()
    {
        if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('Users\Model\UserTable');
        }
        return $this->userTable;
    }		
	
	public function getForgetpasswordTable()
    {
        if (!$this->forgetpasswordTable) {
            $sm = $this->getServiceLocator();
            $this->forgetpasswordTable = $sm->get('users\Model\ForgetpasswordTable');			
        }
        return $this->forgetpasswordTable;
    }
	
	public function getCountryTable()
    {
        if (!$this->countryTable) {
            $sm = $this->getServiceLocator();
            $this->countryTable = $sm->get('Application\Model\CountryTable');			
        }
        return $this->countryTable;
    }
	public function getStateTable()
    {
        if (!$this->stateTable) {
            $sm = $this->getServiceLocator();
            $this->stateTable = $sm->get('Application\Model\StateTable');			
        }
        return $this->stateTable;
    }
	public function getCategoriesACtion(){
		$html="";
		if(isset($_POST['check']) && $_POST['check']!=""){
		$industry_id=$_POST['industry_id'];
		$country_id=$_POST['country_id'];
			if($industry_id!='all'){
				$industries = $this->getIndustryTable()->getindustriesCategories($industry_id);
				if(isset($industries->category_id)){
				  $categoryids=explode(",",$industries->category_id);
				  foreach($categoryids as $categoryvalue){
					 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryvalue);
					 $categories[]=$getallcategories;
				  }
				}
				$html.='<option value="all">Select a category</option>';				
				foreach($categories as $categoriess){
					$html.='<option value="'.$categoriess->category_id.'">'.$categoriess->category_name.'</option>';
				}
				$result = new JsonModel(array(					
					'output' 		=> 'success',
					'categories'		=>	$html,
				));
				return $result;	
			}else{
				$html.='<option value="all">Select a category</option>';	
				$result = new JsonModel(array(					
					'output' 		=> 'success',
					'categories'		=>	$html,
				));
				return $result;	
			}				
		}else if(isset($_POST['user_id']) && $_POST['user_id']!=""){			
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getdetails = $table->editUser($_POST['user_id']);
			if($getdetails!=""){
				$industry_id=$getdetails->industry;
				$country_id=$getdetails->country;
				$industries = $this->getIndustryTable()->getindustry($industry_id);
				if(isset($industries->category_id)){
				  $categoryids=explode(",",$industries->category_id);
				  foreach($categoryids as $categoryvalue){
					 $getallcategories = $this->getCategoriesTable()->getallCategory($categoryvalue);
					 $categories[]=$getallcategories;
				  }
				}
				$html.='<option value="all">Select a category</option>';				
				foreach($categories as $categoriess){
					$html.='<option value="'.$categoriess->category_id.'">'.$categoriess->category_name.'</option>';
				}
				$result = new JsonModel(array(					
					'output' 		=> 'success',
					'categories'		=>	$html,
				));	
				return $result;				
			}else{
				$html.='<option value="all">Select a category</option>';
				$result = new JsonModel(array(					
					'output' 		=> 'not success',
					'categories'		=>	$html,
				));	
				return $result;
			}
		}else{
			$html.='<option value="all">Select a category</option>';
				$result = new JsonModel(array(					
					'output' 		=> 'not success',
					'categories'		=>	$html,
				));	
				return $result;
		}
	} 
	public function getIndustriesAction(){
		$country_id=$_POST['country_id'];
		if($country_id!='all'){
			$html="";
			$html.='<option value="all">Select a industry</option>';	
			$table = $this->getServiceLocator()->get('Products\Model\CountryAndIndustryTable');	
			$industries = $table->getCountryIndustries( $country_id );
			foreach($industries as $industrieslist){
				$html.='<option value="'.$industrieslist->industry_id.'">'.$industrieslist->industryname.'</option>';
			}
		}else{
			$html="";
			$html.='<option value="all">Select a industry</option>';	
		}
		$result = new JsonModel(array(					
			'output' 		=> 'success',
			'industries'	=>	$html,
		));	
		return $result;
	}
	public function getUserRegionsAction(){
		if(isset($_POST['check']) && $_POST['check']=="check"){	
			if(isset($_POST['user_id']) && $_POST['user_id']!="all"){	
				$user_id_user=explode("/",$_POST['user_id']);
				$userId=$user_id_user[0];
				$html="";
				$html.='<option value="all">Select a country</option>';	
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($userId);
				$html.='<option value="'.$getdetails->country.'">'.$getdetails->usercountry.'</option>';				
				$result = new JsonModel(array(					
					'output' 		=> 'success',
					'getallcountries'	=>	$html,
				));	
			}else{
				$html="";
				$html.='<option value="all">Select a country</option>';	
				$result = new JsonModel(array(					
					'output' 		=> 'success',
					'getallcountries'	=>	$html,
				));
			}
			return $result;
		}else if(isset($_POST['user_id']) && $_POST['user_id']!=""){	
			$userId=$_POST['user_id'];
			$html="";
			$html.='<option value="all">Select a country</option>';	
			$getallcountries=$this->getuserRegionsTable()->getuserregions($userId);
			foreach($getallcountries as $regions){
				$html.='<option value="'.$regions->country_id.'">'.$regions->countryname.'</option>';
			}
			$result = new JsonModel(array(					
				'output' 		=> 'success',
				'getallcountries'	=>	$html,
			));	
			return $result;
		}else{
			$html="";
			$html.='<option value="all">Select a country</option>';	
			$result = new JsonModel(array(					
				'output' 		=> 'success',
				'getallcountries'	=>	$html,
			));
			return $result;
		}
	}
	public function checkingProductAction(){
		if(isset($_POST['productid']) && $_POST['productid']!=""){
			$product = $this->getProductsnewTable()->getProducts($_POST['productid']);
			if($product->package>0 ){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}
			if( $product->quntity>0){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}
			if($product->p_status==1){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}
			else{
				$result = new JsonModel(array(					
					'output' 		=> 'notsuccess',
				));
			}
			return $result;
		}
	}
	public function checkIndustryNameAction(){
		if(isset($_POST['ind_name']) && $_POST['ind_name']!=""){
			$checkIndustry = $this->getIndustryTable()->checkIndustryName($_POST['ind_name'],$_POST['country_id']);
			if( $checkIndustry > 0 ){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}else{
				$result = new JsonModel(array(					
					'output' 		=> 'notsuccess',
				));			
			}
		}else{
			$result = new JsonModel(array(					
				'output' 		=> 'success',
			));
		}
		return $result;
	}
	public function checkCategoryNameAction(){
		if(isset($_POST['cat_name']) && $_POST['cat_name']!=""){
			$checkCategoryName = $this->getCategoriesTable()->checkCategoryName($_POST['cat_name']);
			if( $checkCategoryName > 0 ){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}else{
				$result = new JsonModel(array(					
					'output' 		=> 'notsuccess',
				));			
			}
		}else{
			$result = new JsonModel(array(					
				'output' 		=> 'success',
			));
		}
		return $result;
	}
	public function checkCountryNameAction(){
		if(isset($_POST['coun_name']) && $_POST['coun_name']!=""){
			$checkCountryName = $this->getCountryTable()->checkCountryName($_POST['coun_name']);
			if( $checkCountryName > 0 ){
				$result = new JsonModel(array(					
					'output' 		=> 'success',
				));
			}else{
				$result = new JsonModel(array(					
					'output' 		=> 'notsuccess',
				));			
			}
		}else{
			$result = new JsonModel(array(					
				'output' 		=> 'success',
			));
		}
		return $result;
	}
	public function getRegionsAjaxAction(){
		if(isset($_POST['user_id']) && $_POST['user_id']!=""){
			$userId=$_POST['user_id'];
			$html="";
			$html.='<option value="">Select a country</option>';	
			$getallcountries=$this->getuserRegionsTable()->getuserregions($userId);
			foreach($getallcountries as $regions){
				if(isset($_SESSION['products']['origin']) && $_SESSION['products']['origin']==$regions->country_id){
					$html.='<option value="'.$regions->country_id.'/'.$regions->countryname.'" selected="selected">'.$regions->countryname.'</option>';
				}else if(isset($_POST['user_region_id']) && $_POST['user_region_id']==$regions->country_id){
					$html.='<option value="'.$regions->country_id.'/'.$regions->countryname.'" selected="selected">'.$regions->countryname.'</option>';
				}else{
					$html.='<option value="'.$regions->country_id.'/'.$regions->countryname.'">'.$regions->countryname.'</option>';
				}
			}
			$result = new JsonModel(array(					
				'output' 		=> 'success',
				'getallcountries'	=>	$html,
			));	
			return $result;			
		}else{
			$html="";
			$html.='<option value="">Select a country</option>';	
			$result = new JsonModel(array(					
				'output' 		=> 'success',
				'getallcountries'	=>	$html,
			));	
			return $result;			
		}
	}
}

		

	
